完全不知道如何在EF框架中使用外键更新模型(迁移)
本文关键字:更新 模型 迁移 不知道 全不知 框架 EF | 更新日期: 2023-09-27 18:14:51
我使用的是EF 6.1.3最新版本
表关系是一对多
(两个表都有数据,但是调用update-database后外键不反射)
这里两个模型rss_master可以有多个rss_master_publish
[Table("dbo.rss_master")]
public class RssMasterModel
{
[Key]
public int userid { get; set; }
public string prenom {get; set;}
public string nom { get; set; }
public string titre { get; set; }
public string phone { get; set; }
public string phone_direct { get; set; }
public string phone_client { get; set; }
public string phone_other { get; set; }
public string email { get; set; }
public string fax { get; set; }
public string username { get; set; }
public virtual ICollection<RssMasterPublish> rssMasterPublish { get; set; }
}
[Table("dbo.rss_master_publish")]
public class RssMasterPublish
{
[Key]
public int id_publish { get; set; }
public int userid { get; set; }
public string title { get; set; }
public string description { get; set; }
public string imgPath { get; set; }
public DateTime datePublish { get; set; }
[ForeignKey("userid")]
public virtual RssMasterModel rssMasterModel { get; set; }
}
RssMasterPublish是一个手动添加的新表,我想启动控制台并调用update-database并使更改反映在数据库上,但什么都没有发生。如果可能,我不想删除那个表上的数据。
在DbContext上没有太多的配置,这里只是设置
this.Configuration.LazyLoadingEnabled = false;
在Configuration.cs文件中构造函数只添加
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
我对所有的迁移都感到困惑,通常我只是添加新列并更新数据库,但现在我与需要更改的表有关系,所以如果有人能帮助我。
update-database
将把最新的迁移应用到数据库。但是您似乎还没有为RSSMasterPublish
中的外键创建迁移。
你得看看add-migration