我正在遵循msdn上的指南:https://docs.microsoft.com/en-us/ef/core/modeling/relationships#other-relationship-patterns
我需要许多关系的两个类:
public class Initiative
{
public int InitiativeId { get; set; }
//other properties
}
public class AppUser : IdentityUser
{
//properties
}
我的加入课程:
public class UserVolunteer
{
public string userId;
public AppUser user;
public int initiativeId;
public Initiative initiative;
}
我的流利的api代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserVolunteer>()
.HasKey(t => new { t.userId, t.initiativeId });
modelBuilder.Entity<UserVolunteer>()
.HasOne(bc => bc.user)
.WithMany(b => b.UserVolunteers)
.HasForeignKey(bc => bc.userId);
modelBuilder.Entity<UserVolunteer>()
.HasOne(bc => bc.initiative)
.WithMany(c => c.UserVolunteers)
.HasForeignKey(bc => bc.initiativeId);
}
初始化迁移时,出现以下错误:
属性表达式't => new <> f__AnonymousType0`2(userId = t.userId,InitiativeId = t.initiativeId)'无效。该表达式应表示属性访问:“ t => t.MyProperty”。指定多个属性时,请使用匿名类型:'t => new {t.MyProperty1,t.MyProperty2}'。 参数名称:propertyAccessExpression
我不明白是什么问题。我做的和指南中一样,但有错误。
答案 0 :(得分:0)
在类UserVolunteer
中,userId
和initiativeId
是公共字段,而不是属性。因此,它在t => new { t.userId, t.initiativeId }
上引发异常,因为它不是有效的属性访问。
public class UserVolunteer
{
public string userId { get; set; }
public AppUser user { get; set; }
public int initiativeId { get; set; }
public Initiative initiative { get; set; }
}