I am trying to create an sdf with Code-First, however I am getting an error that no Keys are defined for my tables, and cannot understand why.
I have the following Classes :-
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);
}
}
and in the web.config :-
<add name="CommonsContext" connectionString="Data Source=|DataDirectory|Personal.sdf" providerName="System.Data.SqlServerCe.4.0" />
When I browse to the App_Data folder, I cannot even see the Personal.sdf, so it means that its not even created.
What am I doing wrong? Can’t figure out as yet.
Thanks for your help and time
Probably because you haven’t defined getters and setters for your key properties