实体框架 - 上唯一索引UPSERT [英] Entity Framework - UPSERT on unique indexes

查看:142
本文介绍了实体框架 - 上唯一索引UPSERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一下关于我的问题,但找不到任何真正帮助

I searched a bit regarding my problem but can't find anything that really to help.

所以,我的问题/两难困境保持这样的:
我知道mysql数据库具有可以在同一查询使用此格式可用于插入/更新了独特的指标体系:
插入T(A,b,C)值(1,1, 1)上的重复键更新b =值(b),C =值(C);
和用于替换由索引的现有记录一个替换格式

So my problem/dilema stays like this: I know that mysql database have a unique index system that can be used for insert/update in same query using this format: insert into t(a,b,c) values(1,1,1) on duplicate keys update b=values(b),c=values(c); and a replace format used to replace a existing recording by that index.

说实话,我在MSSQL看到的只能是类似的东西是合并,但我真的不喜欢它,并验证查询插入或更新不是基于毕竟唯一索引...

to be honest the only similar stuff that I saw in MSSQL is the merge but I really don't like it at all and verifying a query to insert or update isn't unique index based after all...

所以,我怎么可以模拟mysql的独特UPSERT到实体框架?这是我的主要问题...

So how can I emulate the mysql unique UPSERT into Entity Framework? this is my main problem...

我的意思是没有从实体集获得记录,并检查它是否为空或不为可能的插入或更新;

I mean without getting the record from entity set and checking it if is null or not for a possible insert or update;

我能怎么做呢?或不?任何暗示可能是有用的。

Can I get it? Or not? Any hint can be useful

我看到的这个但不会出现到第6版...实体

I saw this but doesn't appear into version 6...

例如:

    [Table("boats")]
    public class Boat
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int id { get; set; }
        [MaxLength(15)]
        [Index("IX_ProviderBoat",1,IsUnique=true)]
        public string provider_code { get; set; }
        public string name { get; set; }
        [Index("IX_ProviderBoat", 3, IsUnique = true)]
        [MaxLength(50)]
        public string model { get; set; }
        [Index("IX_ProviderBoat", 2, IsUnique = true)]
        [MaxLength(15)]
        [Key]
        public string boat_code { get; set; }
        public string type { get; set; }
        public int built { get; set; }
        public int length { get; set; }            
    }



所以,我想基于使用我IX_ProviderBoat唯一索引更新/插入EF

So I want to update/insert based on the my IX_ProviderBoat unique index using EF

推荐答案

AddOrUpdate 方法 IDBSet ,并在EF6可用。

The AddOrUpdate method is a member of IDBSet and is available in EF6.

AddOrUpdate 方法不是一个原子操作,从多个线程调用并不能保证第二个线程的更新而不是添加再次ING - 这样你就可以得到重复记录。存储

The AddOrUpdate method is not an atomic operation, calls from multiple threads does not guarantee the second thread Update instead of Adding again - so you can get duplicate records stored.

这例子进行了测试,并致力于您的期望:

This example was tested and worked to your expectations:

        Boat boat = new Boat // nullable fields omitted for brevity 
        {
            boat_code = "HelloWorld",
            id = 1,
            name = "Fast Boat",
            built = 1,
            length = 100
        };

        using (BoatContext context = new BoatContext()) // or whatever your context is
        {
            context.Set<Boat>().AddOrUpdate(boat); // <-- IDBSet!!!
            context.SaveChanges();
        }

如果我们改变 boat_code AddOrUpdate()方法将添加一个新的记录。如果 boat_code 为HelloWorld`它将更新现有记录。我相信这是你在找什么...

If we change boat_code the AddOrUpdate() method will add a new record. If the boat_code is 'HelloWorld` it will update the existing record. I believe this is what you are looking for...

希望这有助于!

这篇关于实体框架 - 上唯一索引UPSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆