Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - unmapped entity. Possible?

Is it possible to create an entity that is not mapped to a table in the database?

I created an entity (A) with 5 properties (name, phone, email, date, comment). There is no table named A in my database.

I queried the other tables in my model using linq and convert the result to a list of type (A)

IList<A> results = new IList<A>();
results = (from m in B
           where m.Id < 10
           select new { m.name, m.email, m.date, m.phone, m.comment }).ToList();
return View(results)

I did this to prevent from passing an anonymous type to my view.

like image 245
Kyle Johnson Avatar asked Jun 27 '26 12:06

Kyle Johnson


1 Answers

First, since you are assigning something to variable results you don't need to create a new list. The following will work:

IList<A> results;       // No value yet.

Then you can instantiate A just like you would do otherwise.

results = (from m in B
          where m.Id < 10
          select new A(m.name, m.email, m.date, m.phone, m.comment))
          .ToList();

return View(results)

However, your assumption is wrong here. You say: I convert the result to a list of type A to prevent from passing an anonymous type to my view, but your idea of anonymous types is incorrect.

The following will not create an anonymous type. It will create a very unanonymous IEnumerable<A> or IQueryable<A>:

var results = from m in B
              select new A();

However, the following will create an anonymous type:

var results = from m in B
              select new {
                  Name = m.name,
                  Email = m.email,
                  Date = m.date,
                  Phone = m.phone,
                  Comment = m.comment
              };
like image 179
Daniel A.A. Pelsmaeker Avatar answered Jun 29 '26 22:06

Daniel A.A. Pelsmaeker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!