Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Issue: Conventions :: specifying parameter name for stored procedure via lightweight convention overrides specifying it on entity directly [1640]

$
0
0
Consider the following code:

```
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}

public class MyContext : DbContext
{
public DbSet<MyBaseClass> Classes { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties().Where(p => p.Name == "Name").Configure(c => c.HasParameterName("prm_name"));

modelBuilder.Entity<Foo>().MapToStoredProcedures(c => c.Insert(i => i.Parameter(p => p.Name, "_name")));
modelBuilder.Entity<Foo>().MapToStoredProcedures(c => c.Insert(i => i.Parameter(p => p.DateOfBirth, "_dob")));
}
}
```

this produces the following Foo_Insert function (note the unexpected parameter name)

```
CreateStoredProcedure(
"dbo.Foo_Insert",
p => new
{
prm_name = p.String(),
_dob = p.DateTime(),
},
body:
@"INSERT [dbo].[Foos]([Name], [DateOfBirth])
VALUES (@prm_name, @_dob)

DECLARE @Id int
SELECT @Id = [Id]
FROM [dbo].[Foos]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()

SELECT t0.[Id]
FROM [dbo].[Foos] AS t0
WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id"
);
```
Comments: unexpected behavior also happens when you configure parameter name via FluentAPI: this: modelBuilder.Entity<Foo>().Property(p => p.Name).HasParameterName("prm_name"); wins over this: modelBuilder.Entity<Foo>().MapToStoredProcedures(c => c.Insert(i => i.Parameter(p => p.Name, "_name"))); even though the MapToStoredProcedures was declared after HasParameterName (and last one should always win)

Viewing all articles
Browse latest Browse all 10318

Trending Articles