我正在尝试使用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,所以这意味着它甚至都没有创建。
我做错了什么?还不清楚。
感谢您的帮助和时间
答案 0 :(得分:5)
可能是因为您没有为关键属性定义getter和setter
public int ArticleID { get; set; }
public int CategoryID { get; set; }