实体框架 Fluent api - 根据命名约定创建 forgein 密钥
我开始学习 EF Fluent API。
我有 2 个简单的 POCO 类。
public class Customer
{
public int CustomerId{ get; set;}
public string Name{ get; set;}
}
public class Project
{
public int ProjectId { get; set; }
public int CustomerId { get; set; }
public string Name { get; set; }
public Customer Customer { get; set; }
}
上下文类
public class MyCtx:DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Customer> Authors { get; set; }
public MyCtx(string connString):base(connString)
{}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//PK
modelBuilder.Entity<Project>()
.HasKey(p => p.ProjectId)
.Property(p => p.ProjectId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("PROJECT_ID")
.IsRequired();
modelBuilder.Entity<Project>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
//--------------------------------------------------------------------
//PK
modelBuilder.Entity<Customer>()
.HasKey(c => c.CustomerId)
.Property(c => c.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CUSTOMER_ID")
.IsRequired();
modelBuilder.Entity<Customer>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
base.OnModelCreating(modelBuilder);
}
}
I 定义 CustomerId 将是表 customer 中的主键,ProjectId 将是项目表中的主键。
我对这种行为并不感到惊讶。项目表中的 CustomerId 自动为外键。
这种行为是基于命名约定吗?或者它是如何运作的?
I start learn EF Fluent API.
I have 2 simple POCO classes.
public class Customer
{
public int CustomerId{ get; set;}
public string Name{ get; set;}
}
public class Project
{
public int ProjectId { get; set; }
public int CustomerId { get; set; }
public string Name { get; set; }
public Customer Customer { get; set; }
}
context class
public class MyCtx:DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Customer> Authors { get; set; }
public MyCtx(string connString):base(connString)
{}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//PK
modelBuilder.Entity<Project>()
.HasKey(p => p.ProjectId)
.Property(p => p.ProjectId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("PROJECT_ID")
.IsRequired();
modelBuilder.Entity<Project>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
//--------------------------------------------------------------------
//PK
modelBuilder.Entity<Customer>()
.HasKey(c => c.CustomerId)
.Property(c => c.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CUSTOMER_ID")
.IsRequired();
modelBuilder.Entity<Customer>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
base.OnModelCreating(modelBuilder);
}
}
I defined that CustomerId will be primary key in table customer and ProjectId will be primary key in project table.
I am little surprised with this behavarior. CustomerId in project table is automatically foreign key.
This behavior is based on naming convention? Or how it works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它基于命名约定,在本例中具体为
NavigationPropertyNameForeignKeyDiscoveryConvention
:或者它是
PrimaryKeyNameForeignKeyDiscoveryConvention
:我不确定是哪一个。
如果您不喜欢这些约定之一,可以使用模型生成器删除它们:
所有约定的完整列表是 此处。
Yes, it is based on a naming convention, in this case specifically the
NavigationPropertyNameForeignKeyDiscoveryConvention
:Or it is the
PrimaryKeyNameForeignKeyDiscoveryConvention
:I am not sure which one.
If you don't like one of those conventions you can remove them with the model builder:
A complete list of all conventions is here.