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
451 views
in Technique[技术] by (71.8m points)

stored procedures - Entity framework Code First - configure mapping for SqlQuery

I'm using Entity Framework 5 (with Code First approach) to populate a class of mine from a legacy stored procedure with parameters, and this is working fine (details follow). My problem is that I want to map the columns' names to property with different names (I don't like the names coming from the Erp). I tried to use a Configuration class (like what I do when I map to views or tables) to specify the column name for properties with a different name, and here are my results:

  • if I don't use the configuration class (I don't add it in the OnModelCreating method of the DbContext) then EF works but loads only the properties that match exactly with the name of the columns (and this is what I expected in this case); others property are null;
  • if I use the configuration class (adding it to the modelBuilder in the OnModelCreating method of the DbContext) then EF raises an exception stating that "The data reader is incompatible with the specified '...Item'. A member of the type, 'Description', does not have a corresponding column in the data reader with the same name", and this sounds very strange to me, because in the configuration I specify that the property Description map to the column ItemDescription.

Why the configuration is affecting my result but its specification are not used to map the columns? Is there another way to specify this mapping using SqlQuery?

Here are the details:

My POCO class:

public class Item
    {
        public String Id { get; set; }
        public String Description { get; set; }
    }

The configuration class:

public class ItemConfiguration : EntityTypeConfiguration<Item>
    {
        public ItemConfiguration()
        {
            HasKey(x => new { x.Id });
            Property(x => x.Id).HasColumnName("Code");
            Property(x => x.Description).HasColumnName("ItemDescription");
        }
    }

The stored procedure return the data with the columns "Code" and "ItemDescription"; I call it in this way:

var par = new SqlParameter();
par.ParameterName = "@my_par";
par.Direction = ParameterDirection.Input;
par.SqlDbType = SqlDbType.VarChar;
par.Size = 20;
par.Value = ...;

var data = _context.Database.SqlQuery<Item>("exec spItem @my_par", par);

and with this I add the configuration to the context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
      modelBuilder.Configurations.Add(new ItemConfiguration());
}

Thank you!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I found here:

http://entityframework.codeplex.com/workitem/233?PendingVoteId=233

that "The SqlQuery method is designed not to take any mapping into account ...".

They say also "We agree that it would be useful to have the option to make SqlQuery honor Column attributes so we're keeping this issue open and putting it on our backlog for future consideration.", so, if you have my same problem, please vote :-)


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

...