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

问题描述



所以我的问题/ dilema保持如下:
我知道mysql数据库有一个唯一的索引系统,可以使用以下格式在同一查询中插入/更新:
insert into t(a,b,c)values(1,1, 1)重复键更新b =值(b),c =值(c);
和用于替换该索引的现有录音的替换格式。



说实话,我在MSSQL中看到的唯一类似的东西是 merge ,但我真的不喜欢它,并且验证一个查询插入或更新不是唯一的索引基于所有...



那么我怎么能模仿mysql独特的UPSERT到实体框架?这是我的主要问题...



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



我可以得到吗?或不?任何提示都可以使用



我看到,但不会出现在版本6 ...



实体示例:

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

所以我想根据我的IX_ProviderBoat唯一索引更新/插入使用EF



解决方案

AddOrUpdate 方法是 IDBSet 并且在EF6中可用。



AddOrUpdate 方法不是原子操作来自多个线程的调用不能保证第二个线程更新而不是再次添加,所以你可以获得重复的记录



此示例经过测试,符合您的期望:

 code> Boat boat = new Boat //为简洁起见,省略了可空字段
{
boat_code =HelloWorld,
id = 1,
name =Fast Boat ,
built = 1,
length = 10 0
};

using(BoatContext context = new BoatContext())//或者你的上下文是
{
context.Set< Boat>()。AddOrUpdate(boat); //< - IDBSet !!!
context.SaveChanges();
}

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



希望这有帮助!


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

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.

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...

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

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

example of entity:

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

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

解决方案

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

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

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...

Hope this helps!

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

07-30 03:38