EF6:为实体配置复杂映射(代码优先)

时间:2016-06-16 15:30:40

标签: c# sql .net entity-framework

我想要使用EF6 fluent API配置两个数据库实体。

altscreen on

对于任何配置,当尝试将帐户实体实例插入到这样的数据库中时,我总是会收到错误“无法确定相关操作的有效排序”:

public class Account
{
    public Int32 Id { get; set; }

    public Int32? LastOperationId { get; set; }
    public virtual Operation LastOperation { get; set; }

    public virtual List<Operation> Operations { get; set; }
}

public class Operation
{
    public Int32 Id { get; set; }

    public Int32? AccountId { get; set; }
    public virtual Account Account { get; set; }
}

2 个答案:

答案 0 :(得分:3)

幸运的是,EF推断外键列LastOperationIdmodelBuilder.Entity<Operation>() .HasKey(x => x.Id) .HasOptional(x => x.Account) .WithMany(x => x.Operations); modelBuilder.Entity<Account>() .HasKey(x => x.Id) .HasOptional(x => x.LastOperation); ,所以这对我有用:

row_number()

答案 1 :(得分:2)

这是您在Code-First中所需要的组合:

dateTimePicker1.MinDate = DateTime.Now;

帐户表:columnname:Id

操作表:列名Id(与Account共享),AccountForListId(1..n)