本文介绍了使用ColumnAttribute或HasKey方法指定复合主键的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在2个对象具有父子关系使用复合主键。每当我尝试创建一个新的迁移,我得到一个错误:

I'm trying to use composite primary key on 2 objects with parent-child relationship. Whenever I try to create a new migration, I get an error:

无法确定型复合主键排序'Models.UserProjectRole。使用ColumnAttribute或HasKey方法指定复合主键的顺序。

根据错误提示,我不添加注释列(订单= X)但错误依然存在,不会消失,除非我离开的关键注解只有一个字段。
这是我的目标,它跳开:

As per error suggests, I do add annotation Column (Order = X) but the error still there and does not go away, unless I leave only one field with Key annotation. Here is my object that it trips off:

public class UserProjectRole
{
    [Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserProjectRoleID { get; set; }

    [Key, Column (Order = 1)]
    public Guid ProjectID { get; set; }

    [ForeignKey("ProjectID")]
    public Project Project { get; set; }

    public Guid AppUserGuid { get; set; }

    // followed by a number of unrelated String fields.
 }

下面是项目类:

public class Project: Base
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ProjectID { get; set; }

    public virtual ICollection<UserProjectRole> UserRoles { get; set; }

    // followed by a number of unrelated String fields.

}

下面是我的DbContext的一部分:

Here is part of my DBContext:

public class SiteContext : DbContext
{

    public DbSet<Project> Projects { get; set; }

    public DbSet<UserProjectRole> UserProjectRoles { get; set; }
}



我在VisualStudio的2012年EF 4.3.1

I'm in VisualStudio 2012 with EF 4.3.1

我一直在敲我的头这一段时间,所有的论坛和SO答案,建议补充一点,我已经列顺序标注。
我失去了一些东西明显???

I have been banging my head against this for a while now and all the forum and SO answers suggest to add Column Order annotation that I already have.Am I missing something obvious???

感谢您的阅读远本 - )

推荐答案

花了摆弄和测试不同的东西很多。我是一无所知,直到我决定用从头类似的数据结构的一个新项目的香草。
和当我的NuGet安装的EntityFramework,我被一个消息:

It took a lot of fiddling about and testing different things. I was clueless until I decided to make a new vanilla project with the similar data structure from scratch.And when I installed EntityFramework from NuGet, I was shown a message:

实体框架4.1至4.3包含在

的EntityFramework组装System.ComponentModel.DataAnnotations命名空间中的其他数据注解。在.NET 4.5这些注解移至
处于
System.ComponentModel.DataAnnotations.dll组装
System.ComponentModel.DataAnnotations.Schema命名空间中的.NET Framework的一部分。如果你正在使用
EF 4.x和针对.NET 4.5这将导致两个数据说明
在不同的组件相同的名称。因为在
.NET Framework中的注解是在一个不同的命名空间,我们没能
利用类型转发来避免这种冲突。

据可以在.NET 4.5使用EF 4.x的,但我们建议如果不使用受影响
数据注解使用
EF 5的最新预发布版本有您的代码没有任何影响。如果您使用的是
数据说明在C#项目可以使用extern修饰符至
确保您的代码使用从EntityFramework.dll
注释(http://msdn.microsoft.com /en-us/library/e59b22c5(v=VS.80).aspx)。如果
你在.NET 4.5的
System.ComponentModel.DataAnnotations.dll装配使用新的注解,他们
不会被代码首先进行处理。

It is possible to use EF 4.x on .NET 4.5 but we recommend using the latest pre-release version of EF 5. If you are not using the affected data annotations there is no impact on your code. If you are using the data annotations in a C# project you can use the extern modifier to ensure your code uses the annotations from EntityFramework.dll (http://msdn.microsoft.com/en-us/library/e59b22c5(v=VS.80).aspx). If you use the new annotations from the System.ComponentModel.DataAnnotations.dll assembly in .NET 4.5 they will not be processed by Code First.

受影响的注解是:



  • 的ComplexType

  • DatabaseGenerated

  • DatabaseGeneratedOption

  • ForeignKey的

  • InverseProperty

  • 最大长度

  • MINLENGTH个

  • NotMapped


  • Column
  • ComplexType
  • DatabaseGenerated
  • DatabaseGeneratedOption
  • ForeignKey
  • InverseProperty
  • MaxLength
  • MinLength
  • NotMapped
  • Table

在这一点上,我意识到,我的数据项目在VS2012刚创建,是由默认定位。网4.5和我的解决方案项目的其余部分是从VS2010和定位.NET 4.0迁移。
所以,我更新了所有项目面向.NET 4.5,得到了5.0的EntityFramework的预发布。

At that point I realised that my data project was freshly created in VS2012 and was by default targeting .Net 4.5 and the rest of my project in solutions were migrated from VS2010 and targeting .Net 4.0. So I have updated all the projects to target .Net 4.5 and got the pre-release of EntityFramework 5.0.

确保您更新项目首先NET4.5,以后做更新EF到5.0,否则会恨你一辈子,许多兔子会死。

的是一个很好的入门更新到EF5.0

This screencast is a great starter for updating to EF5.0

I have changed targetFrameworkto "net45" and now I'm getting the expected behavior from migrations. I guess there would be a better way to get nuget target .Net 4.5 with packages, but that's what worked for me.

我希望这将节省的人在敲打墙壁的头。

I hope this will save somebody banging their head on a wall.

这篇关于使用ColumnAttribute或HasKey方法指定复合主键的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 07:27