开发者

LINQ to SQL with Lazy Loading map classes correcly

开发者 https://www.devze.com 2023-02-27 14:36 出处:网络
this is first time i am doing this, when i want to add new item to the 开发者_运维技巧db it fails.

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();
    }
}


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

[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; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号