2016-02-01 2 views
0

Я делаю веб-сервер с MVC4. У меня есть 3 класса - User, MobileApp, Device. Классы пользователей и устройств имеют отношение «один ко многим» (такие же, как классы пользователей и MobileApp). Я использую свободно API для отображения классов:Entity Framework - Fluent API - свойство навигации не объявлено propety по типу

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 
} 

Mapping

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

ответ

0

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