Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

c# - How to avoid adding duplicate data to db when using EFCore.BulkExtensions?

I am using EFCore.BulkExtensions in .NET Core 3.1 with Entity Framework Core.

I have a list containing records that already exist in my database, as well as records that are new and do not yet exist in my database.

I want to only add the new records to my database. I am currently managing to add the new records but I am also unfortunately adding the old ones, meaning duplicates are made in the table.

I have a domain object called InstagramInsightDatum that looks like this and a table in my database to reflect it:

public int InstagramInsightDatumId { get; set; }
public int Value { get; private set; }
public DateTime EndTime { get; private set; }
public int InstagramInsightId { get; set; }
public InstagramInsight InstagramInsight { get; set; }

I want to only add new InstagramInsightDatums to the database when they are unique. I will never need to update these records, just add new ones. At the moment my code looks like this:

enter image description here

I've highlighted the line where I add these datums to the table where the duplicates are happening in red - here you can see I am making the bulkInsert with the InstagramInsightDatums, and setting the PropertiesToExclude to InstagramInsightDatumId (I am not 100% how this config is meant to work but I assumed this feature is to allow us to ignore certain properties when comparing data?) - as removing the PK from the properties should reveal any potential duplicates...

Can anyone see why this isn't working?

question from:https://stackoverflow.com/questions/65617480/how-to-avoid-adding-duplicate-data-to-db-when-using-efcore-bulkextensions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...