There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many The One-to-Many relationship Write some codes first: class Company { public int CompanyId { get; set; } [MaxLength(50)] public string CompanyName { get;…
Joins allow developers to combine data from multiple tables into a sigle query. Let's have a look at codes: Creating a project Create a project named JoinTest Add Packages by NuGet Create entities: public class Person { public int PersonId { get; set…
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two clinets update the same entity, one of theirs data will be lost without any notify. Some times, the client want to know if his data has been saved suc…
Sometimes, you need to find some data in an existing context instead of the database. By befault, Entity Framework always find data in database. If you want to find data which have loaded in memory, please do it like this: Frist of all, let's insert…
Creating Entities First of all, Let's create some entities to have a test. Create a project Add following packages by NuGet EntityFramework MySql.Data.Entity (I'm just using MySql, it's not necessary) Add some codes: class Class { public int ClassId…
There are three ways to define the database structure by Entity Framework API. They are: Attributes The DbModelBuilder API Configuration Classes Attributes We can Add some attributes for entities or proteries to configure database structures. For exa…
Sometimes, you have created two models. They have the same parent class like this: public class Person { public int PersonId { get; set; } public string PersonName { get; set; } } public class InsidePerson : Person { public string Title { get; set; }…
Complex types are classes that map to a subset of columns of a table.They don't contains key. They are the Value Objects. For example, you have a entity named Company: public class Company { public int CompanyId { get; set; } public string CompanyNam…
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way to do this. Add a class named MyCommandInterceptor class MyCommandInterceptor: DbCommandInterceptor { public override void NonQueryExecuted(DbCommand…
Create a new project named MySqlTest Install following packages by right-clicking on the References folder of the project and selecting Manage NuGet Packages... EntityFramework MySql.Data MySql.data.Entity Update the app.config file Add a model and t…