实体框架 - Fluent API - 导航属性不是类型的声明属性

时间:2016-02-01 01:52:39

标签: c# asp.net-mvc-4 entity-framework-6 ef-fluent-api

我正在使用MVC4做一个Web服务器。我有3个类 - 用户,MobileApp,设备。用户和设备类具有一对多关系(与User和MobileApp类相同)。我使用流畅的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方法列出用户时,我得到以下内容:

“导航属性'DeviceName'不是'Device'类型的声明属性。验证它是否未从模型中明确排除,并且它是有效的导航属性。

1 个答案:

答案 0 :(得分:0)

我认为DeviceMapping应该是this.Property(e =&gt; e.DeviceName).IsRequired(),并且可能与LastLocal化相同。