gpt4 book ai didi

entity-framework - System.Data.Entity.Edm.EdmEntityType 没有定义键

转载 作者:行者123 更新时间:2023-12-02 05:41:57 30 4
gpt4 key购买 nike

我正在尝试使用 Code-First 创建一个 sdf,但是我收到一个错误,指出没有为我的表定义键,并且无法理解为什么。

我有以下类(class):-

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,这意味着它甚至没有创建。

我做错了什么?还想不通。

感谢您的帮助和时间

最佳答案

可能是因为您还没有为关键属性定义 getter 和 setter

public int ArticleID { get; set; }

public int CategoryID { get; set; }

关于entity-framework - System.Data.Entity.Edm.EdmEntityType 没有定义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10830632/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com