10

I have a stored procedure in my sqlserver database which is returning multiple resultset.

I am using following link from msdn to read multiple resultset from SP in entity framework.

https://msdn.microsoft.com/en-us/library/jj691402(v=vs.113).aspx

To read data, I need to have DBSets in xyzDBContext class for each of the resultsets.

Model Class:

public class AreaView
{
    public String Area { get; set; }
    public String Weight { get; set; }

}

DBContext:

 public class EnsembleAPIContext : DbContext
    {
       public DbSet<AreaView> area {get; set;}

// I want to prevent this table from getting created in db

      }

This is how I am reading resultset and mapping it with above created dbset.

 reader.NextResult();
                    var contributionArea = ((IObjectContextAdapter)db)
                       .ObjectContext
                       .Translate<ContributionArea>(reader, "area ", MergeOption.AppendOnly);

What I need to do is I want to create entity for these resultsets, but I dont want framework to create tables for these entities in database.

Note: Reason for doing this is, resultset which is returned by sp doesnt have a primary key, so suppose we can have a valid table created using entity without PK.

Is this possible?

Any help is appreciated.

6
  • 1
    Why do you need a DbSet? You generally create a method on your DbContext to get data from an SP, see stackoverflow.com/questions/20901419/… Commented Aug 3, 2017 at 11:51
  • @CodeCaster SP is not created by Codefirst. It already exists. I am just calling it from Code. Commented Aug 3, 2017 at 11:53
  • 1
    I'm afraid you didn't understand my comment. It has nothing to do with code first. You use DbSet<T> for tables, not for SPs. Commented Aug 3, 2017 at 11:58
  • @CodeCaster Did you check that msdn link, I am following that to read multiple resultset. Now while reading resultset, we need to mention entity for which resultset is to be mapped. Now here I want to create an entity just for this mapping purpose, and not create table in database for that. I hope this is clear Commented Aug 3, 2017 at 12:00
  • 3
    No, I didn't read that link. Now I did, and I still don't see the need for a DbSet. Read How to Ask and include all relevant details in your question. Commented Aug 3, 2017 at 12:04

2 Answers 2

24

The answer to "If we can skip creating tables in database with Entity framework is": Yes

Use [NotMapped] attribute.

 [NotMapped]
   public class Employee
    {
        public int ID { get; set; }
        public String name { get; set; }
    }

You can use this model for general purpose and table wont be created for this in database.

Another way of doing this is

In OnModelCreating() method

modelBuilder.Ignore<Employee>();

This way DBContext will ignore creating table for this model.

Sign up to request clarification or add additional context in comments.

Comments

8

Can we create a DBSet<> without creating corresponding table in database

No. DbSet<T> represents a real database table or view.

To read data, I need to have DBSets in xyzDBContext class for each of the resultsets.

You don't. The ObjectContext.Translate method can be used to map DbReader to any class. The linked example is using entity types, but there is another Translate method overload which works for any type as described in Directly Executing Store Commands MSDN topic - Materializing the Result Type section.

With that being said, remove the DbSet from your context and use something like this:

var areaView = ((IObjectContextAdapter)db).ObjectContext.Translate<AreaView>(reader);

11 Comments

Thank you for clarifying. So EF will create tables for the model classes?? So how doees it identity them? Just curious.
There are several ways used by EF to discover class as being entity. One of them is when you have defined DbSet<SomeClass> :) Others are through navigation properties or flient API calls like modelBuilder.Entity<SomeClass>() etc.
I found the way to skip creating tables. Please check my answer. Thanks for your time.
Yes, you can tell EF to ignore the entity, but then DbSet makes no sence - there is nothing you can do with it. It will be confusing and most likely throw exceptions if you try using it in a query or something. So I stay on my point in the answer.
But in my situation where I need models just to map resultset, there is not point to create dbset and tables. Just have to use models, in these cases ignoring will be useful. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.