实体框架 Fluent api - 根据命名约定创建 forgein 密钥

发布于 2024-12-13 03:08:00 字数 1960 浏览 0 评论 0原文

我开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-12-20 03:08:00

是的,它基于命名约定,在本例中具体为 NavigationPropertyNameForeignKeyDiscoveryConvention

发现名称为 a 的外键属性的约定
依赖导航属性名称(在您的情况下为 Customer)和
主体类型主键属性名称(在您的情况下为 CustomerId)。

或者它是PrimaryKeyNameForeignKeyDiscoveryConvention

发现名称匹配的外键属性的约定
主体类型主键属性名称。

我不确定是哪一个。

如果您不喜欢这些约定之一,可以使用模型生成器删除它们:

modelBuilder.Conventions
    .Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
// etc.

所有约定的完整列表是 此处

Yes, it is based on a naming convention, in this case specifically the NavigationPropertyNameForeignKeyDiscoveryConvention:

Convention to discover foreign key properties whose names are a
combination of the dependent navigation property name (Customer in your case) and the
principal type primary key property name(s) (CustomerId in your case).

Or it is the PrimaryKeyNameForeignKeyDiscoveryConvention:

Convention to discover foreign key properties whose names match the
principal type primary key property name(s).

I am not sure which one.

If you don't like one of those conventions you can remove them with the model builder:

modelBuilder.Conventions
    .Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
// etc.

A complete list of all conventions is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文