【问题标题】:Entity Framework - Fluent API - The navigation property is not a declared propety on type实体框架 - Fluent API - 导航属性不是类型上的声明属性
【发布时间】:2016-02-01 01:52:39
【问题描述】:

我正在使用 MVC4 做一个 Web 服务器。我有 3 个类 - 用户、移动应用程序、设备。 User 和 Device 类具有一对多的关系(与 User 和 MobileApp 类相同)。我正在使用 fluent 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' 上的声明属性。验证它没有明确地从模型中排除,并且它是一个有效的导航属性。

【问题讨论】:

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


    【解决方案1】:

    在我的脑海中,我认为 DeviceMapping 应该是 this.Property(e => e.DeviceName).IsRequired() 并且可能与 LastLocalization 相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2016-06-23
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多