gpt4 book ai didi

c# - Entity Framework : Help Creating Database using Code first Approach

转载 作者:搜寻专家 更新时间:2023-10-30 23:43:46 27 4
gpt4 key购买 nike

好的2个问题:1. 我是数据库的新手,我想学习代码优先的方法来创建数据库。我已经多次使用模型优先方法。任何人都可以帮助我使用代码优先方法对这个数据库进行编码吗(如果可能的话,我也想添加一个 Employee 表)?这是数据库图的链接:

http://databaseanswers.org/data_models/customers_and_orders/images/customers_and_orders_ref_data_model.gif

  1. 此外,我将如何一次性插入客户表/客户地址地址,当然是使用 Entity Framework ?

预先感谢任何愿意提供帮助的人。

最佳答案

你可以像下面那样做:

请注意此解决方案是作为控制台应用程序完成的。

请首先添加以下类作为代码执行此操作:

        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);

关于c# - Entity Framework : Help Creating Database using Code first Approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com