如何避免在Entity Framework 4.3.1中重复插入

编程入门 行业动态 更新时间:2024-10-10 13:22:50
本文介绍了如何避免在Entity Framework 4.3.1中重复插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

b

public class City { public City() { Posts = new List<Post>(); } public City(string cityName) { Name = cityName; } public virtual ICollection<Post> Posts { get; private set; } public int Id { get; set; } public string Name { get; private set; } }

A 发布类代表邮政编码和城市参考的组合

A Post class represents combination of zip code and city reference

public class Post { public virtual City City { get; set; } public int Id { get; set; } public string ZipCode { get; set; } }

这两个实体的上下文中定义了它们的集合作为其配置

both entities have their sets defined in context as their configurations

public DbSet<City> Cities { get; set; } public DbSet<Post> Posts { get; set; } modelBuilder.Configurations.Add(new CityMap()); modelBuilder.Configurations.Add(new PostMap()); public class CityMap : EntityTypeConfiguration<City> { public CityMap() { // Primary Key HasKey(t => t.Id); // Properties // Table & Column Mappings ToTable("City"); Property(t => t.Id).HasColumnName("Id"); Property(t => t.Name).HasColumnName("Name"); } } public class PostMap : EntityTypeConfiguration<Post> { public PostMap() { // Primary Key HasKey(t => t.Id); // Properties // Table & Column Mappings ToTable("Post"); Property(t => t.Id).HasColumnName("Id"); Property(t => t.ZipCode).HasColumnName("ZipCode"); // Relationships HasRequired(t => t.City) .WithMany(t => t.Posts) .Map(map=>map.MapKey("CityId")); } }

我已经创建了使用这些对象进行操作的类,静态方法获取或创建对象并将其返回给调用者。

I've created class for manipulation with those objects with static methods which get or creates objects and return them to caller.

private static City GetCity(string cityName) { City city; using (var db = new DbContext()) { city = db.Cities.SingleOrDefault(c => c.Name == cityName); if (city == null) { city = new City(cityName); db.Cities.Add(city); db.SaveChanges(); } } return city; } private static Post GetPost(string zipCode, string cityName) { Post post; City city = GetCity(cityName); using (var db = new DbContext()) { post = db.Posts.SingleOrDefault(p => p.City.Id == city.Id && p.ZipCode == zipCode); if (post == null) { post = new Post { City = city, ZipCode = zipCode }; // State of city is unchanged db.Posts.Add(post); // State of city is Added db.SaveChanges(); } } return post; }

想像一下,我调用方法

GetPost("11000","Prague");

方法 GetCity 已启动,如果不存在,方法创建一个城市,然后调用 SaveChanges()方法。

method GetCity is started and if not exists, method creates a city and then calls the SaveChanges() method.

如果我将返回的 city 实体设置为新的 Post 实例,Entity Framework将生成第二个插入相同的城市。

If I set returned city entity to new Post instance, Entity Framework generates a second insert for the same city.

如何避免这种行为?我只想在上一步中创建或加载引用的 city post c> post c

How can I avoid this behavior? I want to only insert new post entity with referenced city created or loaded in previous step.

推荐答案

当您将其附加到未更改的

You need to set the State of your city when you attach it to unchanged

context.Entry(city).State = EntityState.Unchanged;

更多推荐

如何避免在Entity Framework 4.3.1中重复插入

本文发布于:2023-10-19 17:46:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Entity   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!