使用ColumnAttribute或HasKey方法指定复合主键的顺序 [英] Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys

查看:243
本文介绍了使用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:


实体框架4.x和。 .NET Framework 4.5



实体框架4.1至4.3包含
中的附加数据注释
EntityFramework程序集中的System.ComponentModel.DataAnnotations命名空间。在.NET 4.5中,
System.ComponentModel.DataAnnotations.dll程序集的
System.ComponentModel.DataAnnotations.Schema命名空间中的这些注释被移动到
。如果您使用
EF 4.x并定位到.NET 4.5,则会导致两个数据注释
在不同的程序集中具有相同的名称。因为
中的注释是.NET框架在不同的命名空间中,我们无法使用类型转发来避免这种冲突。

Known Issues with Entity Framework 4.x and .NET Framework 4.5

Entity Framework 4.1 thru 4.3 included additional data annotations in the System.ComponentModel.DataAnnotations namespace in the EntityFramework assembly. In .NET 4.5 these annotations were moved to be part of the .NET Framework in the System.ComponentModel.DataAnnotations.Schema namespace of the System.ComponentModel.DataAnnotations.dll assembly. If you are using EF 4.x and targeting .NET 4.5 this results in two data annotations with the same name in different assemblies. Because the annotations in the .NET Framework are in a different namespace we were not able to use type forwarding to avoid this conflict.

它可以在.NET 4.5上使用EF 4.x,但是我们建议您使用
最新的预发行版本EF 5.如果您不使用受影响的
数据注释,那么对您的代码没有影响。如果您在C#项目中使用
数据注释,则可以使用extern修饰符
确保您的代码使用EntityFramework.dll
的注释( http://msdn.microsoft.com/en-us/library/e59b22c5(v = VS 0.80)的.aspx )。如果
您使用.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


在这一点上,我的错误已经消失,但我变得不一样了。我无法添加迁移,因为它无法找到迁移配置,即使我已经配置了几秒钟之前。
再次花了很多钱与NuGet,卸载广告重新安装包。
然后我看到package.config这样的行:

At that point my error was gone, but I was getting different one. I could not add migration because it could not find Migration Configuration, even though I had configuration set up just seconds before it. That again took a lot of fiddling with NuGet, uninstalling ad re-installing packages. Then I saw in packages.config lines like this:

package id="EntityFramework" version="5.0.0-rc" targetFramework="net40" 

我已经将targetFramework更改为net45,现在我从迁移中获取预期的行为。我想会有一个更好的方法来获取nuget目标.Net 4.5与包,但这是对我有用的。

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方法指定复合主键的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆