【发布时间】:2015-04-24 07:30:35
【问题描述】:
我在尝试创建 0..1 到 0..1 的关系时遇到问题
在扩展 applicationuser 类和 MovieModel 的 AspNetUsers 表之间首先使用代码。
关系的本质是用户可以租用或不租用一部电影,并且
电影可以租给一个租客,也可以不租。每个实体都可以独立存在
而当没有电影租借或租借者时,外国领域只是nulls
我的模型:
public class MovieModel
{
[Key]
public int MovieId { get; set; }
[Required]
[StringLength(50)]
[Column(TypeName="varchar")]
[Display(Name = "Movie Name")]
public string MovieName { get; set; }
[Display(Name = "Release Year")]
public string RenterId { get; set; }
[ForeignKey("RenterId")]
public virtual ApplicationUser Renter { get; set; }
应用用户类
public class ApplicationUser : IdentityUser
{
public int? MovieId { get; set; }
[ForeignKey("MovieId")]
public virtual MovieModel Movie{ get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
从逻辑上讲,它应该可以工作。有两个外键。 ApplicationUser 类(AspNetUser 表)中的一个外键和MovieModel 中的一个外键
不知何故,我在添加迁移时收到此错误:Unable to determine the principal end of an association between the types 'VideoLibrary.Models.ApplicationUser' and 'VideoLibrary.Models.MovieModel'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotation
为什么会出现这种错误?我不需要关系中的主要实体。他们都可以独立。
提前致谢
【问题讨论】:
标签: entity-framework asp.net-mvc-5 ef-code-first