实体框架 - 流畅 API - 导航属性不是在类型上声明的属性
本文关键字:属性 类型 声明 流畅 API 导航 实体 框架 | 更新日期: 2023-09-27 18:32:37
我正在使用MVC4做一个Web服务器。我有3类 - 用户,移动应用程序,设备。用户和设备类具有一对多关系(与用户和移动应用类相同)。我正在使用流畅的 API 来映射类:
public class User
{
public string UserID { get; set; }
public string UserLogin { get; set; }
public string UserPassword { get; set; }
public virtual ICollection<MobileApp> MobileApps { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
public class Device
{
public int DeviceID { get; set; }
public string DeviceName { get; set; }
public bool StolenFlag { get; set; }
public int BatteryLevel { get; set; }
public DbGeography LastLocalization { get; set; }
public int UserID { get; set; } //foreign key for user
public virtual User User { get; set; }
}
public class MobileApp
{
public int MobileAppId { get; set; }
public string MobileAppIID { get; set; }
public string MobileAppToken { get; set; }
public virtual User User { get; set; }
public int UserID { get; set; } //foreign key for user
}
映射
public class UserMapping : EntityTypeConfiguration<User>
{
public UserMapping():base()
{
this.HasKey(e => e.UserID);
this.HasRequired(e => e.UserLogin);
this.HasRequired(e => e.UserPassword);
this.HasMany<Device>(e => e.Devices).WithRequired(e => e.User).HasForeignKey(e => e.UserID);
this.HasMany<MobileApp>(e => e.MobileApps).WithRequired(e => e.User).HasForeignKey(e => e.UserID);
this.ToTable("User");
}
}
public class DeviceMapping:EntityTypeConfiguration<Device>
{
public DeviceMapping():base()
{
this.HasKey(e => e.DeviceID);
this.HasRequired(e => e.DeviceName);
this.HasRequired(e => e.LastLocalization);
this.ToTable("Device");
}
}
public class MobileAppMapping:EntityTypeConfiguration<MobileApp>
{
public MobileAppMapping():base()
{
this.HasKey(e => e.MobileAppId);
this.Property(e => e.MobileAppIID).HasMaxLength(150);
this.Property(e => e.MobileAppToken).HasMaxLength(150);
this.ToTable("MobileApp");
}
}
上下文:
public class AccountContext : DbContext
{
public AccountContext() : base("AccountDb")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Device> Devices { get; set; }
public DbSet<MobileApp> MobileApps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new UserMapping());
modelBuilder.Configurations.Add(new MobileAppMapping());
modelBuilder.Configurations.Add(new DeviceMapping());
}
}
初始 化:
public class AccountInitializer : DropCreateDatabaseIfModelChanges<AccountContext>
{
protected override void Seed(AccountContext context)
{
User user = new User() { UserLogin = "Test", UserPassword = "Test" };
Device device = new Device() { DeviceName = "Dev1", BatteryLevel = 100, StolenFlag = false, UserID = 1, LastLocalization = DbGeography.FromText("POINT(52.403371 16.955098)")};
MobileApp MobApp = new MobileApp() { MobileAppIID = "IID", MobileAppToken = "Token", UserID = 1 };
}
}
当我尝试使用 ToList 方法列出用户时,我得到以下例外:
"导航属性'设备名称'不是类型'设备'上的声明属性。验证它是否尚未从模型中显式排除,并且它是有效的导航属性。
在我的头顶上,我认为设备映射应该是这个。属性(e => e.设备名称)。IsRequired(),对于 LastLocalization,可能也是如此。