Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# ReferentialConstraint中的依赖属性映射到存储生成的列。专栏:';规格ID';_C#_Entity Framework_Asp.net Mvc 5_Entity Framework Migrations - Fatal编程技术网

C# ReferentialConstraint中的依赖属性映射到存储生成的列。专栏:';规格ID';

C# ReferentialConstraint中的依赖属性映射到存储生成的列。专栏:';规格ID';,c#,entity-framework,asp.net-mvc-5,entity-framework-migrations,C#,Entity Framework,Asp.net Mvc 5,Entity Framework Migrations,我有一个类似的问题,与现有的标题相同的问题,但它略有不同 我得到一个错误: 引用约束中的依赖属性映射到存储生成的列。列:“规格ID”。 当我尝试保存到数据库时: var doc = new Specification(); doc.ProjectComponentID = spec.ProjectComponentID; doc.Description = spec.Description; db.Specifications.Add(doc); db.SaveChanges(); 创建数据

我有一个类似的问题,与现有的标题相同的问题,但它略有不同

我得到一个错误:

引用约束中的依赖属性映射到存储生成的列。列:“规格ID”。

当我尝试保存到数据库时:

var doc = new Specification();
doc.ProjectComponentID = spec.ProjectComponentID;
doc.Description = spec.Description;
db.Specifications.Add(doc);

db.SaveChanges();
创建数据库时,我使用seed方法映射1对1或无关系:

modelBuilder.Entity<Specification>()
            .HasRequired(pc => pc.ProjectComponent)
            .WithOptional(s => s.Specification);
modelBuilder.Entity()
.has必需(pc=>pc.ProjectComponent)
.可选(s=>s.规格);
我的两个相关实体是:

public class ProjectComponent
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProjectComponentID { get; set; }

    public int ProjectID { get; set; }
    public virtual Project Project { get; set; }

    public int ProjectPhaseID { get; set; }
    public virtual ProjectPhase Phase { get; set; }

    ... other properties ...

    public Nullable<int> SpecificationID { get; set; }
    public virtual Specification Specification { get; set; }
}

public class Specification
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SpecificationID { get; set; }

    public int ProjectComponentID { get; set; }
    public virtual ProjectComponent ProjectComponent { get; set; }

    public string Description { get; set; }

}
公共类项目组件
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int ProjectComponentID{get;set;}
公共int ProjectID{get;set;}
公共虚拟项目{get;set;}
public int ProjectPhaseID{get;set;}
公共虚拟项目阶段{get;set;}
…其他属性。。。
公共可空规范ID{get;set;}
公共虚拟规范{get;set;}
}
公共类规范
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int规范ID{get;set;}
public int ProjectComponentID{get;set;}
公共虚拟项目组件项目组件{get;set;}
公共字符串说明{get;set;}
}
我需要能够添加一个规范到项目组件。使用ProjectComponentID提供规范,但自动生成规范ID

我在创建数据库时没有收到任何错误,只是在尝试保存规范时

所以我找到了“A”解决方案。但我不相信这是“最好”的解决方案

显然,CodeFirstEF需要在一对零或一关系中共享主键

为了解决这个问题,我删除了每个实体上的外键:

public class ProjectComponent
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProjectComponentID { get; set; }

    public int ProjectID { get; set; }
    public virtual Project Project { get; set; }

    public int ProjectPhaseID { get; set; }
    public virtual ProjectPhase Phase { get; set; }

    ... other properties ...

    public virtual Specification Specification { get; set; }

}

public class Specification
{
    public int SpecificationID { get; set; }
    public virtual ProjectComponent ProjectComponent { get; set; }

    public string Description { get; set; }
}
我将Fluent API修改为与前一个相反的版本:

modelBuilder.Entity<ProjectComponent>()
            .HasOptional(pc => pc.Specification)
            .WithRequired(s => s.ProjectComponent);
请评论是否有更好的解决方案。。。但这是我能找到的唯一一种方法,作为纯代码优先管理的DB来解决它

var comp = db.ProjectComponents.Find(spec.SpecificationID);
comp.Specification = new Specification {Description = spec.Description};

db.SaveChanges();