实体框架:帮助使用代码优先的方法创建数据库
本文关键字:方法 创建 数据库 代码 框架 帮助 实体 | 更新日期: 2023-09-27 18:06:07
OK 2道题:1. 我是新的数据库,我想学习代码的第一种方法来创建数据库。我已经使用过几次模型优先的方法。谁能帮助我与编码这个数据库使用代码优先的方法(我也想添加一个员工表,如果可能的话)?下面是DB图的链接:
http://databaseanswers.org/data_models/customers_and_orders/images/customers_and_orders_ref_data_model.gif- 另外,我如何插入,比如说客户表/客户地址地址,一次完成,当然使用实体框架?
对任何愿意帮忙的人提前表示感谢。
您可以这样做:
请注意,此解决方案已作为控制台应用程序完成。
请先添加以下类作为代码:
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; }
}
当你使用代码优先方法时,你需要从你创建的类中创建模型,它可以这样做:
上下文类应该如下所示:
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);
-
对于Employee表,您可以创建一个类(Employee.cs):
公共类Employees{
公共字符串FName {get;set;}公共字符串LName {get;set;}public string Position {get;set;}公共字符串Email {get;set;}[显示(Name = "全名")]公共字符串FullName{得到{返回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();