导航属性作为主键 [英] Navigation Properties as Primary Keys

查看:127
本文介绍了导航属性作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF 6与MVC 5.我有一个类定义如下:

  public class ConEdSignup 
{
[Key,Column(Order = 0)]
public virtual ApplicationUser Attendee {get;组;
[Key,Column(Order = 1)]
public virtual ConEdSession ConEdSession {get;组; }
[必需]
public DateTime SignupTime {get;组; }
[必需]
public bool Attended {get;组; }
}

这基本上是一个多对多关系的链接表,我有关于这种关系的附加属性。当我尝试为此创建一个迁移时,它会给我错误Models.ConEdSignup::EntityTypeConEdSignup'没有键定义该EntityType的键。



我清楚地定义了它的键,但它不喜欢它。如何使用这些导航属性作为主键?

解决方案

您应该在MSDN上阅读本文: -



http://msdn.microsoft。 com / en-gb / data / jj679962.aspx



它解释了Entity Framework遵守的约定。它还解释了如何创建自己的自定义约定。



您需要以特定方式设置您的课程,以便EF识别您尝试实现的功能。


I'm using EF 6 with MVC 5. I have a class defined as follows:

public class ConEdSignup
{
    [Key, Column(Order = 0)]
    public virtual ApplicationUser Attendee { get; set; }
    [Key, Column(Order = 1)]
    public virtual ConEdSession ConEdSession { get; set; }
    [Required]
    public DateTime SignupTime { get; set; }
    [Required]
    public bool Attended { get; set; }
}

This is basically a link table for a many-to-many relationship where I have additional properties about that relationship. When I try to create a migration for this, it gives me the error "Models.ConEdSignup: : EntityType 'ConEdSignup' has no key defined. Define the key for this EntityType."

I clearly defined the keys for it, but it doesn't like it. How can I use these navigation properties as the primary keys?

解决方案

You should read this article on MSDN :-

http://msdn.microsoft.com/en-gb/data/jj679962.aspx

It explains the conventions that Entity Framework adheres too. It also explains how to create you own custom conventions.

You need to setup your classes in a specific way so that EF recognises what your trying to achieve.

这篇关于导航属性作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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