本文介绍了使用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方法来指定复合主键的顺序。

根据错误建议,我添加注释列(Order = 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

我一直在抨击我的头一会儿,所有的论坛和答案建议添加我已经拥有的列顺序注释。
我缺少一些明显的东西?

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???

谢谢你阅读这个 - / p>

Thank you for reading this far -)

推荐答案

它花了很多时间来研究和测试不同的东西。直到我决定从头开始制作一个类似数据结构的新的香草项目时,我才是无能为力的。
当我从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:

它可以在.NET 4.5上使用EF 4.x,但是我们建议您使用
最新的预发行版本EF 5.如果您不使用受影响的
数据注释,那么对您的代码没有影响。如果您在C#项目中使用
数据注释,则可以使用extern修饰符
确保您的代码使用EntityFramework.dll
的注释()。如果
您使用.NET 4.5中的
System.ComponentModel.DataAnnotations.dll程序集中的新注释,那么
将不会被Code First处理。

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

  • 最大长度

  • 最小长度

  • 未映射


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

在这一点上,我意识到我的数据项目是在VS2012中新创建的,默认情况下是定位。 Net 4.5和我的解决方案的其余部分从VS2010迁移并定位到.Net 4.0。
所以我已经更新了所有的项目来定位.Net 4.5并获得了EntityFramework 5.0的预发行。

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-16 09:12