به روز رسانی سادهتر اجزاء ارتباطات در EF Code first به کمک GraphDiff
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۵/۱۸ ۱۱:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
PM> Install-Package RefactorThis.GraphDiff
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace GraphDiffTests.Models
{
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public virtual ICollection<Tag> Tags { set; get; } // many-to-many
[ForeignKey("UserId")]
public virtual User User { get; set; }
public int UserId { get; set; }
public BlogPost()
{
Tags = new List<Tag>();
}
}
public class Tag
{
public int Id { set; get; }
[StringLength(maximumLength: 450), Required]
public string Name { set; get; }
public virtual ICollection<BlogPost> BlogPosts { set; get; } // many-to-many
public Tag()
{
BlogPosts = new List<BlogPost>();
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<BlogPost> BlogPosts { get; set; } // one-to-many
}
} using System;
using System.Data.Entity;
using GraphDiffTests.Models;
namespace GraphDiffTests.Config
{
public class MyContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }
public DbSet<Tag> Tags { get; set; }
public MyContext()
: base("Connection1")
{
this.Database.Log = sql => Console.Write(sql);
}
}
} using System.Data.Entity.Migrations;
using System.Linq;
using GraphDiffTests.Models;
namespace GraphDiffTests.Config
{
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyContext context)
{
if(context.Users.Any())
return;
var user1 = new User {Name = "User 1"};
context.Users.Add(user1);
var tag1 = new Tag { Name = "Tag1" };
context.Tags.Add(tag1);
var post1 = new BlogPost { Title = "Title...1", Content = "Content...1", User = user1};
context.BlogPosts.Add(post1);
post1.Tags.Add(tag1);
base.Seed(context);
}
}
}
using (var context = new MyContext())
{
var user1 = new User { Id = 1, Name = "User 1_1_1" };
var post1 = new BlogPost { Id = 1, Title = "Title...1_1", Content = "Body...1_1",
User = user1, UserId = user1.Id };
var tags = new List<Tag>
{
new Tag {Id = 1, Name = "Tag1_1"},
new Tag {Id=12, Name = "Tag2_1"},
new Tag {Name = "Tag3"},
new Tag {Name = "Tag4"},
};
tags.ForEach(tag => post1.Tags.Add(tag));
context.UpdateGraph(post1, map => map
.OwnedEntity(p => p.User)
.OwnedCollection(p => p.Tags)
);
context.SaveChanges();
}
public void Update(Article entity)
{
var item = articles.Find(entity.Id);
item.Author = entity.Author;
item.CategoryId = entity.CategoryId;
item.Content = entity.Content;
item.Source = entity.Source;
item.Title = entity.Title;
if (entity.FileStream != null)
item.FileStream = new File
{
ContentType=entity.FileStream.ContentType,
Id=entity.Id,
Size = entity.FileStream.Size,
FileBytes = entity.FileStream.FileBytes
};
var allTag = tags.ToCacheableList().ToList();
var tagsList = entity.Tags.ToList().Select(x => x.Name.ToPersianContent(true)).ToArray();
if (entity.Tags != null && entity.Tags.Any())
{
entity.Tags.Clear();
entity.Tags = new Collection<Tag>();
}
var listOfTags = tagsList.Select(tag =>
allTag.Any(x => x.Name == tag) ?
allTag.FirstOrDefault(x => x.Name == tag) :
new Tag { Name = tag }).ToList();
listOfTags.ForEach(tag => entity.Tags.Add(tag));
unitOfWork.PwsUpdateGraph(entity, map => map
.OwnedEntity(p => p.FileStream)
.OwnedCollection(p => p.Tags));
} RowVersion failed optimistic concurrency