【问题标题】:Fluent Api Entity Framework coreFluent Api 实体框架核心
【发布时间】:2018-06-11 14:15:54
【问题描述】:

一个用户可以有 1 个或 0 个帐户

public class User
    {
        public int UserId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public Account Account { get; set; }
    }

    public class Account
    {
        public int AccountId { get; set; }         
        public DateTime CreatedDateTime { get; set; }
        public User User { get; set; }

    }

这是使用 Entity Framework 6 的流畅 api 代码

public class ClassDbContext: DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<User>()
                  .HasOptional(s => s.Account) 
                  .WithRequired(ad => ad.User);
    }
    public DbSet<User> Users { get; set; }
    public DbSet<Account> Accounts { get; set; }
}

这是ResultImage的结果

使用 Entity Framework Core 等效的 fluent api 代码是什么?

【问题讨论】:

    标签: c# entity-framework entity-framework-core ef-fluent-api


    【解决方案1】:

    @Tseng 很接近,但还不够。使用建议的配置,您将收到异常消息:

    无法确定在“Account.User”和“User.Account”之间检测到的一对一关系的子/依赖方。要识别关系的子/依赖方,请配置外键属性。详情请见http://go.microsoft.com/fwlink/?LinkId=724062

    基本上在链接的documentation中都有解释。

    首先,您需要使用HasOneWithOne

    其次,您必须使用HasForeignKey指定两个实体中的哪一个是依赖(当没有定义单独的FK属性时无法自动检测到在其中一个实体中)。

    第三,没有更多的必需的依赖IsRequired 方法可用于指定当依赖实体使用单独的 FK 时是否需要 FK(而不是像您的情况那样使用 PK,即使用所谓的 共享主键关联,因为PK 显然不能为空)。

    话虽如此,贴出模型正确的F Core fluent配置如下:

    modelBuilder.Entity<User>()
        .HasOne(e => e.Account)
        .WithOne(e => e.User)
        .HasForeignKey<Account>(e => e.AccountId);
    

    结果是:

    migrationBuilder.CreateTable(
        name: "User",
        columns: table => new
        {
            UserId = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            Email = table.Column<string>(nullable: true),
            Name = table.Column<string>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_User", x => x.UserId);
        });
    
    migrationBuilder.CreateTable(
        name: "Account",
        columns: table => new
        {
            AccountId = table.Column<int>(nullable: false),
            CreatedDateTime = table.Column<DateTime>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Account", x => x.AccountId);
            table.ForeignKey(
                name: "FK_Account_User_AccountId",
                column: x => x.AccountId,
                principalTable: "User",
                principalColumn: "UserId",
                onDelete: ReferentialAction.Cascade);
        });
    

    【讨论】:

      【解决方案2】:

      几乎相同,但有其他名称。

      modelBuilder.Entity<User>()
          .HasOne(s => s.Account) 
          .WithOne(ad => ad.User)
          .IsRequired(false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-03
        • 2020-05-10
        相关资源
        最近更新 更多