简体   繁体   English

实体框架存储过程

[英]Entity Framework Stored Procedure

I am building an Excel import feature that allows the user to specify which columns map to which property of the object. 我正在构建一个Excel导入功能,该功能允许用户指定哪些列映射到对象的哪个属性。 Then using reflection, I create a new object, set the property values and insert the new object into the DbSet. 然后使用反射创建一个新对象,设置属性值,然后将新对象插入DbSet中。 However, some of the new objects may be duplicates and should update an existing record instead of being added to the DbSet as a new object. 但是,某些新对象可能是重复的,应该更新现有记录,而不是作为新对象添加到DbSet中。

The Excel sheet will not contain a column for the object's ID. Excel工作表将不包含对象ID的列。 So I'm wanting to add an Index for a combination of other fields (such as FirstName and LastName). 因此,我想为其他字段(例如名字和姓氏)的组合添加索引。 I want to then pass in a list of objects to the stored procedure like passing in a table parameter; 然后,我想将对象列表传递给存储过程,就像传递表参数一样。 so I can select all the records from the database where the indexes match. 因此我可以从数据库中选择索引匹配的所有记录。

How can I pass in a list of objects into an Entity Framework stored procedure? 如何将对象列表传递到Entity Framework存储过程中?

DbSet is already a list objects to which you can add objects. DbSet已经是可以向其添加对象的列表对象。 If you have a functional key that uniquely identifies each object, then you should be able to find and update it using LINQ. 如果您具有唯一标识每个对象的功能键,则应该能够使用LINQ查找和更新它。

You can pass them through a comma separated list (string), as long as it's less than 8K characters. 您可以将它们通过逗号分隔的列表(字符串)传递,只要它少于8K个字符。 Then you can split them through this function. 然后,您可以通过此功能将它们拆分。 (The function would be used in the SP). (该功能将在SP中使用)。

CREATE PROCEDURE ...
@NameList Varchar(8000)

Select * from People 
where (FirstName+'-'+LastName) in dbo.Split(@NameList,',')
....

The function to split values base on a delimiter chararter 根据定界符来分割值的功能

CREATE FUNCTION [dbo].[Split]
(
    @Input VARCHAR(8000),
    @Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN
(
    WITH Split(stpos,endpos)
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@Input) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@Input,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT  SUBSTRING(@Input,stpos,COALESCE(NULLIF(endpos,0),LEN(@Input)+1)-stpos) As Item
    FROM Split
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM