LINQ to SQL 正确地延迟加载映射类

发布于 2024-11-02 22:24:00 字数 1985 浏览 0 评论 0原文

这是我第一次这样做,当我想向数据库添加新项目时,它失败了。

如何设置文章类中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-11-09 22:24:00

我遇到了 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.

三寸金莲 2024-11-09 22:24:00

找到解决方案,感谢史蒂文·桑德森

[Table(Name="Articles")]
public class Article
{        
    [HiddenInput(DisplayValue=false)]
    [Column(IsPrimaryKey=true, IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
    public int ArticleId { get; set; }

    [Column]
    internal int ArticleCategoryId { get; set; }

    internal EntityRef<ArticleCategories> _ArticleCategoryId;

    //[Column(AutoSync = AutoSync.OnInsert, Name = "ArticleCategoryId")]
    [Association(ThisKey="ArticleCategoryId", Storage="_ArticleCategoryId")]
    public ArticleCategories ArticleCategory
    {
        get
        {
            return _ArticleCategoryId.Entity;
        }
        set
        {
            ArticleCategoryId = value.ArticleCategoryId;
            _ArticleCategoryId.Entity = value;                
        }
    }

    [Column]
    public string Label { get; set; }

    [DataType(DataType.MultilineText)]
    [Column]
    public string Text { get; set; }

    [Required(ErrorMessage = "Musíte vybrat kategorii článku.")]
    [DataType(DataType.DateTime)]
    [Column]
    public DateTime Created { get; set; }
}

found solution, thanks to Steven Sanderson

[Table(Name="Articles")]
public class Article
{        
    [HiddenInput(DisplayValue=false)]
    [Column(IsPrimaryKey=true, IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
    public int ArticleId { get; set; }

    [Column]
    internal int ArticleCategoryId { get; set; }

    internal EntityRef<ArticleCategories> _ArticleCategoryId;

    //[Column(AutoSync = AutoSync.OnInsert, Name = "ArticleCategoryId")]
    [Association(ThisKey="ArticleCategoryId", Storage="_ArticleCategoryId")]
    public ArticleCategories ArticleCategory
    {
        get
        {
            return _ArticleCategoryId.Entity;
        }
        set
        {
            ArticleCategoryId = value.ArticleCategoryId;
            _ArticleCategoryId.Entity = value;                
        }
    }

    [Column]
    public string Label { get; set; }

    [DataType(DataType.MultilineText)]
    [Column]
    public string Text { get; set; }

    [Required(ErrorMessage = "Musíte vybrat kategorii článku.")]
    [DataType(DataType.DateTime)]
    [Column]
    public DateTime Created { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文