Entity Framework:帮助使用代码优先方法创建数据库
Entity Framework: Help Creating Database using Code first Approach
好的2个问题:
1. 我是数据库的新手,我想学习代码优先的方法来创建数据库。我已经多次使用模型优先方法。任何人都可以帮助我使用代码优先方法对这个数据库进行编码吗(如果可能的话,我还想添加一个 Employee table)?
这是数据库图表的 link:
- 此外,我如何一次性插入客户 table /客户地址地址,当然是使用 entity framework?
提前感谢任何愿意提供帮助的人。
对于员工 Table,您可以创建一个 class (Employee.cs):
public class 员工
{
public 字符串 FName {get;set;}
public 字符串 LName {get;set;}
public 字符串位置 {get;set;}
public 字符串电子邮件 {get;set;}
[显示(名称 = "Full Name")]
public 字符串全名
{
得到
{
return LName + ", " + FName;
}
}
}
对于插入,你可以这样做:
var users = new List<User>
{
new User{FName ="Chris", LName ="Fajardo",Position=@"Dev",Email="test.test@test.ae"}
};
users.ForEach(s => context.User.Add(s));
context.SaveChanges();
您可以按如下方式进行:
请注意此解决方案已作为控制台应用程序完成。
请添加以下内容 class 以首先作为代码执行此操作:
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string CustomerOtherDetails { get; set; }
}
public class CustomerAddress
{
[ForeignKey("Customer")]
public int CustomerId { get; set; }
[ForeignKey("AddressType")]
public int AddressTypeId { get; set; }
[ForeignKey("Address")]
public int AddressId { get; set; }
[Key]
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public virtual Customer Customer { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual Address Address { get; set; }
}
public class AddressType
{
[Key]
public int AddressTypeId { get; set; }
public string AddressTypeDescriptiom { get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string City { get; set; }
}
当您使用代码优先方法时,您需要根据您创建的 class 创建模型,并且可以按如下方式完成:
上下文 class 应该如下所示:
public class CustomerContext : DbContext
{
public CustomerContext()
: base("DBConnectionString")
{
//If model change, It will re-create new database.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CustomerContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set primary key to Customer table
modelBuilder.Entity<Customer>().HasKey(m => m.CustomerId);
//set primary key to Address table
modelBuilder.Entity<CustomerAddress>().HasKey(m => m.DateFrom);
modelBuilder.Entity<AddressType>().HasKey(m => m.AddressTypeId);
modelBuilder.Entity<Address>().HasKey(m => m.AddressId);
//Set foreign key property
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.Customer)
.WithMany().HasForeignKey(t => t.CustomerId);
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.AddressType)
.WithMany().HasForeignKey(t => t.AddressTypeId);
modelBuilder.Entity<CustomerAddress>()
.HasRequired(t => t.Address)
.WithMany()
.HasForeignKey(t => t.AddressId);
}
数据库创建和客户插入地址如下:
static void Main(string[] args)
{
using (var ctx = new CustomerContext())
{
//ctx.Database.Create(); // This command can be used to create the database using the code first class
ctx.CustomerAddresses.Add(new CustomerAddress
{
AddressType = new AddressType
{
AddressTypeId = 1,
AddressTypeDescriptiom = "Test"
},
Customer = new Customer
{
CustomerId = 1,
FirstName = "Customer 1"
},
Address = new Address
{
Line1 = "Line 1",
City = "USA"
},
DateFrom = DateTime.Now,
DateTo = DateTime.Now
});
ctx.SaveChanges();
}
}
连接字符串应如下所示:
<connectionStrings>
<add name="DBConnectionString"
connectionString="Data Source=(local);Initial Catalog=CustomerDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
请注意以下注意上面的代码需要以下参考。
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
我试过了,效果如你所料。请让我知道您是否需要这方面的示例项目。
希望对您有所帮助。
已编辑:
要先在代码中配置自引用,请执行以下操作:
public class Product
{
[Key]
public int ProductId { get; set; }
public int? ParentProductId { get; set; }
public virtual Product ParentProduct { get; set; }
}
在OnModelCreating
方法中添加以下代码行:
modelBuilder.Entity<Product>().HasKey(m => m.ProductId);
modelBuilder.Entity<Product>().
HasOptional(e => e.ParentProduct).
WithMany().
HasForeignKey(m => m.ParentProductId);
好的2个问题: 1. 我是数据库的新手,我想学习代码优先的方法来创建数据库。我已经多次使用模型优先方法。任何人都可以帮助我使用代码优先方法对这个数据库进行编码吗(如果可能的话,我还想添加一个 Employee table)? 这是数据库图表的 link:
- 此外,我如何一次性插入客户 table /客户地址地址,当然是使用 entity framework?
提前感谢任何愿意提供帮助的人。
对于员工 Table,您可以创建一个 class (Employee.cs):
public class 员工 {
public 字符串 FName {get;set;} public 字符串 LName {get;set;} public 字符串位置 {get;set;} public 字符串电子邮件 {get;set;} [显示(名称 = "Full Name")] public 字符串全名 { 得到 { return LName + ", " + FName; } }
}
对于插入,你可以这样做:
var users = new List<User> { new User{FName ="Chris", LName ="Fajardo",Position=@"Dev",Email="test.test@test.ae"} }; users.ForEach(s => context.User.Add(s)); context.SaveChanges();
您可以按如下方式进行:
请注意此解决方案已作为控制台应用程序完成。
请添加以下内容 class 以首先作为代码执行此操作:
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string CustomerOtherDetails { get; set; }
}
public class CustomerAddress
{
[ForeignKey("Customer")]
public int CustomerId { get; set; }
[ForeignKey("AddressType")]
public int AddressTypeId { get; set; }
[ForeignKey("Address")]
public int AddressId { get; set; }
[Key]
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public virtual Customer Customer { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual Address Address { get; set; }
}
public class AddressType
{
[Key]
public int AddressTypeId { get; set; }
public string AddressTypeDescriptiom { get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string City { get; set; }
}
当您使用代码优先方法时,您需要根据您创建的 class 创建模型,并且可以按如下方式完成:
上下文 class 应该如下所示:
public class CustomerContext : DbContext
{
public CustomerContext()
: base("DBConnectionString")
{
//If model change, It will re-create new database.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CustomerContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set primary key to Customer table
modelBuilder.Entity<Customer>().HasKey(m => m.CustomerId);
//set primary key to Address table
modelBuilder.Entity<CustomerAddress>().HasKey(m => m.DateFrom);
modelBuilder.Entity<AddressType>().HasKey(m => m.AddressTypeId);
modelBuilder.Entity<Address>().HasKey(m => m.AddressId);
//Set foreign key property
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.Customer)
.WithMany().HasForeignKey(t => t.CustomerId);
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.AddressType)
.WithMany().HasForeignKey(t => t.AddressTypeId);
modelBuilder.Entity<CustomerAddress>()
.HasRequired(t => t.Address)
.WithMany()
.HasForeignKey(t => t.AddressId);
}
数据库创建和客户插入地址如下:
static void Main(string[] args)
{
using (var ctx = new CustomerContext())
{
//ctx.Database.Create(); // This command can be used to create the database using the code first class
ctx.CustomerAddresses.Add(new CustomerAddress
{
AddressType = new AddressType
{
AddressTypeId = 1,
AddressTypeDescriptiom = "Test"
},
Customer = new Customer
{
CustomerId = 1,
FirstName = "Customer 1"
},
Address = new Address
{
Line1 = "Line 1",
City = "USA"
},
DateFrom = DateTime.Now,
DateTo = DateTime.Now
});
ctx.SaveChanges();
}
}
连接字符串应如下所示:
<connectionStrings>
<add name="DBConnectionString"
connectionString="Data Source=(local);Initial Catalog=CustomerDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
请注意以下注意上面的代码需要以下参考。
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
我试过了,效果如你所料。请让我知道您是否需要这方面的示例项目。
希望对您有所帮助。
已编辑:
要先在代码中配置自引用,请执行以下操作:
public class Product
{
[Key]
public int ProductId { get; set; }
public int? ParentProductId { get; set; }
public virtual Product ParentProduct { get; set; }
}
在OnModelCreating
方法中添加以下代码行:
modelBuilder.Entity<Product>().HasKey(m => m.ProductId);
modelBuilder.Entity<Product>().
HasOptional(e => e.ParentProduct).
WithMany().
HasForeignKey(m => m.ParentProductId);