Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF6:为实体配置复杂映射(代码优先)_C#_Sql_.net_Entity Framework - Fatal编程技术网

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

C# EF6:为实体配置复杂映射(代码优先),c#,sql,.net,entity-framework,C#,Sql,.net,Entity Framework,我有两个db实体要使用EF6 fluent API进行配置 public class Account { public Int32 Id { get; set; } public Int32? LastOperationId { get; set; } public virtual Operation LastOperation { get; set; } public virtual List<Operation> Operations { ge

我有两个db实体要使用EF6 fluent API进行配置

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; }
}
公共类帐户
{
公共Int32 Id{get;set;}
公共Int32?LastOperationId{get;set;}
公共虚拟操作LastOperation{get;set;}
公共虚拟列表操作{get;set;}
}
公营课运作
{
公共Int32 Id{get;set;}
public Int32?AccountId{get;set;}
公共虚拟帐户{get;set;}
}
对于任何配置,在尝试将帐户实体实例插入数据库时,我总是会遇到一个错误“无法确定依赖操作的有效顺序”,如下所示:

var account = new Account();
var operation = new Operation();

account.Operations = new List<Operation>() { operation };
account.LastOperation = operation;

dbContext.Accounts.Add(account);
dbContext.SaveChanges();
var账户=新账户();
var操作=新操作();
account.Operations=newlist(){operation};
account.LastOperation=操作;
dbContext.Accounts.Add(account);
dbContext.SaveChanges();

幸运的是,EF推断出外键列
AccountId
LastOperationId
,所以这对我来说很有用:

modelBuilder.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);
modelBuilder.Entity()
.HasKey(x=>x.Id)
.has可选(x=>x.Account)
.具有多个(x=>x.操作);
modelBuilder.Entity()
.HasKey(x=>x.Id)
.has可选(x=>x.LastOperation);

这是您首先需要的代码组合:

public class Account
{
    // One to one to one relationship (shared PK)
     public int Id { get; set; }

     // One to one to one relationship (shared PK)
     public virtual Operation Operation { get; set; }

    // One to many relationship foreign Key
    [InverseProperty("AccountForList")]
     public virtual List<Operation> Operations { get; set; }
   }

   public class Operation
   {
    // One to one to one relationship (shared PK)
    [ForeignKey("Account")]
     public Int32 Id { get; set; }

     // One to one to one relationship (shared PK)
     public virtual Account Account { get; set; }

     // One to many relationship foreign Key
     public Int32? AccountForListId { get; set; }

     // One to many relationship foreign Key
     [ForeignKey("AccountForListId")]
     public virtual Account AccountForList { get; set; }
     }
公共类帐户
{
//一对一关系(共享主键)
公共int Id{get;set;}
//一对一关系(共享主键)
公共虚拟操作{get;set;}
//一对多关系外键
[反向属性(“AccountForList”)]
公共虚拟列表操作{get;set;}
}
公营课运作
{
//一对一关系(共享主键)
[外汇钥匙(“账户”)]
公共Int32 Id{get;set;}
//一对一关系(共享主键)
公共虚拟帐户{get;set;}
//一对多关系外键
公共Int32?AccountForListId{get;set;}
//一对多关系外键
[ForeignKey(“AccountForListId”)]
公共虚拟帐户AccountForList{get;set;}
}
科目表:列名称:Id

操作表:列名Id(与帐户共享)、AccountForListId(1..n)