存储过程的参数少于关联对象

时间:2014-11-20 20:20:58

标签: c# entity-framework stored-procedures entity-framework-6

我的数据库中有一个存储过程(sp_create_user),用于在Users表中创建用户。这是我的存储过程定义:

ALTER PROCEDURE sp_create_user
    @Username nvarchar(50),
    @Password nvarchar(50),
AS
BEGIN
    -- SQL STATMENTS
END

c#中的User对象是:

public partial class User
{
    public User()
    {
    }

    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

这是我将用户实体插入的EF映射到该存储过程:

modelBuilder.Entity<User>().MapToStoredProcedures(
  s =>
      s.Insert(i => i.HasName("sp_create_user"))
);

当我尝试执行新的用户创建时,我得到一个例外,即发送到存储过程的参数数量太大。在跟踪查询之后,我看到它向存储过程发送了User类具有的所有字段。

如何告诉EF仅发送存储过程的相关字段?

1 个答案:

答案 0 :(得分:1)

...
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public int UserId { get; set; }

public string Username { get; set; }
public string Password { get; set; }

[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public string FirstName { get; set; }

[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public string LastName { get; set; }
...

如果您不想修改课程,也可以尝试以下操作:

modelBuilder.Entity<User>().Ignore(u => u.UserId);
相关问题