LINQ to SQL 正确地延迟加载映射类
这是我第一次这样做,当我想向数据库添加新项目时,它失败了。
如何设置文章类中的ArticleCategoryId属性来引用文章中的文章类别,因为此时它为空并且不包含任何值?我的课程如下:
文章类:
[Table(Name="Articles")]
public class Article
{
[HiddenInput(DisplayValue=false)]
[Column(IsPrimaryKey=true, IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
public int ArticleId { get; set; }
[Column(Name="ArticleCategoryId", AutoSync = AutoSync.Never)]
[Mapping.Association(IsForeignKey = true, Name = "FK_Articles_ArticleCategorys")]
public ArticleCategories ArticleCategory{get;set;}
[Column]
public string Label { get; set; }
[DataType(DataType.MultilineText)]
[Column]
public string Text { get; set; }
[DataType(DataType.DateTime)]
[Column]
public DateTime Created { get; set; }
}
类别类:
[Table(Name = "ArticleCategorys")]
public class ArticleCategories
{
[HiddenInput(DisplayValue = false)]
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, Name="ArticleCategoryId")]
public int ArticleCategoryId { get; set; }
[Column]
public string Name { get; set; }
[Column]
public string Description { get; set; }
[DataType(DataType.DateTime)]
[Column] public DateTime Created { get; set; }
}
我的错误:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ArticleId")] Article article)
{
try
{
ArticleCategories category = articleCategoriesRepository.ArticleCategories.FirstOrDefault(x => x.ArticleCategoryId == article.ArticleCategory.ArticleCategoryId);
article.ArticleCategory = category;
article.Created = DateTime.Now;
articlesRepository.SaveArticle(article);
ViewData["Message"] = NudaJeFuc.WebUI.Properties.Resources.text_ArticleSaved;
return RedirectToAction("Index");
}
catch
{
return view();
}
}
this is first time i am doing this, when i want to add new item to the db it fails.
How shall set property of ArticleCategoryId in article class to reference the article category in article as at the moment it is null and does not contain any value? My classes is as follows:
Class for articles:
[Table(Name="Articles")]
public class Article
{
[HiddenInput(DisplayValue=false)]
[Column(IsPrimaryKey=true, IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
public int ArticleId { get; set; }
[Column(Name="ArticleCategoryId", AutoSync = AutoSync.Never)]
[Mapping.Association(IsForeignKey = true, Name = "FK_Articles_ArticleCategorys")]
public ArticleCategories ArticleCategory{get;set;}
[Column]
public string Label { get; set; }
[DataType(DataType.MultilineText)]
[Column]
public string Text { get; set; }
[DataType(DataType.DateTime)]
[Column]
public DateTime Created { get; set; }
}
Class for categories:
[Table(Name = "ArticleCategorys")]
public class ArticleCategories
{
[HiddenInput(DisplayValue = false)]
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, Name="ArticleCategoryId")]
public int ArticleCategoryId { get; set; }
[Column]
public string Name { get; set; }
[Column]
public string Description { get; set; }
[DataType(DataType.DateTime)]
[Column] public DateTime Created { get; set; }
}
My error:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ArticleId")] Article article)
{
try
{
ArticleCategories category = articleCategoriesRepository.ArticleCategories.FirstOrDefault(x => x.ArticleCategoryId == article.ArticleCategory.ArticleCategoryId);
article.ArticleCategory = category;
article.Created = DateTime.Now;
articlesRepository.SaveArticle(article);
ViewData["Message"] = NudaJeFuc.WebUI.Properties.Resources.text_ArticleSaved;
return RedirectToAction("Index");
}
catch
{
return view();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了 Linq2SQL 映射的问题,当您向数据库添加新列时,它不会更新,因此会失败。
因此,我必须删除 .edmx 文件,并在对数据库进行更改时重新添加它。
ADO.NET 实体框架不会发生这种情况,所以如果可以的话请尝试一下。
I ran into an issue with Linq2SQL mapping, where it does not update when you add new columns to the database, and therefore will fail.
So I would have to delete the .edmx file and re-add it anytime I made changes to the database.
This doesn't happen with ADO.NET Entity Framework, so try that if you can.
找到解决方案,感谢史蒂文·桑德森
found solution, thanks to Steven Sanderson