Entity Framework - Fluent API - свойство навигации не объявлено как тип объекта

Я делаю веб-сервер с MVC4. У меня 3 класса - Пользователь, MobileApp, Устройство. Классы User и Device имеют отношение "один ко многим" (так же, как классы 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());

    }

}

Initializer:

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 "не является объявленным свойством типа" Устройство ". Убедитесь, что оно не было явно исключено из модели и является допустимым свойством навигации.

1 ответ

Вдобавок ко всему, я думаю, что DeviceMapping должно быть следующим. Свойство (e => e.DeviceName).IsRequired() и, вероятно, то же самое для LastLocalization.

Другие вопросы по тегам