我正在尝试使用 Code-First 创建一个 sdf,但是我收到一个错误,即没有为我的表定义任何键,并且无法理解为什么。
我有以下课程:-
public class Article
{
[Key]
public int ArticleID;
//[Display(Name = "Title")]
//[MaxLength(255)]
//[Required(ErrorMessage="Title is required")]
public string ArticleTitle;
//[Display(Name = "Date")]
//[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true, NullDisplayText = "")]
//[Required(ErrorMessage = "Date is required")]
public DateTime ArticleDate;
//[Display(Name = "Text")]
//[Required(ErrorMessage = "Text is required")]
public string ArticleText;
//[Display(Name = "Source")]
public string ArticleSource;
//[Display(Name = "Category")]
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
[Key]
public int CategoryID;
[Display(Name = "Category Title")]
[MaxLength(255)]
[Required(ErrorMessage = "Category Title is required")]
public string CategoryTitle;
}
public class CommonsContext : DbContext
{
public DbSet<Article> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//A one-to-one relationship between the Article and Category tables.
modelBuilder.Entity<Article>().HasRequired(x => x.Category);
}
}
在 web.config 中:-
<add name="CommonsContext" connectionString="Data Source=|DataDirectory|Personal.sdf" providerName="System.Data.SqlServerCe.4.0" />
当我浏览到 App_Data 文件夹时,我什至看不到 Personal.sdf,所以这意味着它甚至没有被创建。
我究竟做错了什么?目前还想不通。
感谢您的帮助和时间