Trying to traverse tables from sql database using entity framework targeting .net 3.5 websites - entity-framework

I'm simply trying to get data from two sql server db tables using ado.net entity framework. My code is:
using (Model.Entities e = new Model.Entities())
{
return e.PAGE.First().CONTROL;
}
The database is setup to have two tables, a control table which links to a page table via an 'id' field in the tables (control_id). There is one CONTROL object for each PAGE object.
I keep getting a null value for my return value and I know that's not right.
I can use vis studio and breakpoints to see that there is a PAGE object in 'e' and I can see that there are multiple CONTROL objects in 'e'. This isn't a large database, I just have some sample data in there to ensure that I get this working - so I know that there should be a CONTROL object connected to this PAGE (i've verified this through sql server).
I am very familiar with the general code syntax, I've been using LINQ for a couple of years; however, I have not done much work at all with the entity framework or ado.net 4.
It seems like if I just pull individual table data then it works fine (i.e. e.PAGE.First() .. or .. e.CONTROL.Where(x=>x.someValue.Equals('someValue') ) but if I try to pull by traversing through the tables then I get nothing back (NULL).
Hope that all makes sense.

Some questions for you:
I assume is a 1..1 between PAGE and CONTROL,
Is there a FK called "ControlID" on PAGE?
Do you have a navigational property called "Control" on your "Page" entity in your EDMX?
If the answer to all of the above is Yes, then this should work:
var page = e.Pages.Include("Control").First();
Here, you are returning the First "Page" record, and eager loading the associated control.
The SQL produced should be something like this:
SELECT p.*, c.*
FROM Page p
INNER JOIN Control c
on p.ControlId = c.ControlId

Related

How to generate Views using databasefirst and entity framework?

I've been given a Visual Studio solution to get up and running again for development. The project uses Entity Framework database first. When I generate model from database, the sql wants to convert all of the views into tables. I know views should be avoided with EF, but what is the best way to correct this issue given that the developer no longer works for us?
Thanks
There is nothing wrong with using views with Entity Framework. I assume you mean to say when you update from database it is trying to convert the views to model objects. This is totally fine. often times you are required to specify the primary key for the view. By default Entity Framework will try to use every non null property as the key. in the context menu for each property you can toggle this.
Are you getting an error?
I've come to the conclusion that there is not a way to recreate view entities in your edmx back into actual sql server database views. What needs to happen is that you would generate the sql from the model and run that sql in sql server management studio query analyzer. Delete the tables created that should have been views and figure out what query to write to recreate the views in sql server as they should be. Once that's done, the views in your model should be fine and update there after when running "Update from Database".
Say you have two tables - Categories and Products. You want to create a view called ProductsWithCategoryName. When you do update from database and this view gets added to your EDMX file, viewing the XML shows the following:
<EntitySet Name="ProductsWithCategoryName" EntityType="NorthwindModel.Store.ProductsWithCategoryName" store:Type="Views" store:Schema="dbo" store:Name="ProductsWithCategoryName">
<DefiningQuery>
SELECT
[ProductsWithCategoryName].[ProductID] AS [ProductID],
[ProductsWithCategoryName].[ProductName] AS [ProductName],
[ProductsWithCategoryName].[UnitsInStock] AS [UnitsInStock],
[ProductsWithCategoryName].[CategoryName] AS [CategoryName]
FROM [dbo].[ProductsWithCategoryName] AS [ProductsWithCategoryName]
</DefiningQuery>
</EntitySet>
Problem with the above is that the defining query is NOT the query that creates that view. For you to get the proper defining query you must actually edit the EDMX file by hand adding the proper query to it as follows:
<EntitySet Name="ProductsWithCategoryName" EntityType="NorthwindModel.Store.ProductsWithCategoryName" store:Type="Views" store:Schema="dbo" store:Name="ProductsWithCategoryName">
<DefiningQuery>
SELECT dbo.Products.ProductID, dbo.Products.ProductName, dbo.Products.UnitsInStock, dbo.Categories.CategoryName
FROM dbo.Categories INNER JOIN
dbo.Products ON dbo.Categories.CategoryID = dbo.Products.CategoryID
</DefiningQuery>
</EntitySet>
This still will not give you the expected outcome of a proper sql view getting created. Basically EF when going from the conceptual to the database only produces an table per entity and views are simply seen as another entity.

Entity Framework : map duplicate tables to single entity at runtime?

I have a legacy database with a particular table -- I will call it ItemTable -- that can have billions of rows of data. To overcome database restrictions, we have decided to split the table into "silos" whenever the number of rows reaches 100,000,000. So, ItemTable will exist, then a procedure will run in the middle of the night to check the number of rows. If numberOfRows is > 100,000,000 then silo1_ItemTable will be created. Any Items added to the database from now on will be added to silo1_ItemTable (until it grows to big, then silo2_ItemTable will exist...)
ItemTable and silo1_ItemTable can be mapped to the same Item entity because the table structures are identical, but I am not sure how to set this mapping up at runtime, or how to specify the table name for my queries. All inserts should be added to the latest siloX_ItemTable, and all Reads should be from a specified siloX_ItemTable.
I have a separate siloTracker table that will give me the table name to insert/read the data from, but I am not sure how I can use this with entity framework...
Thoughts?
You could try to use the Entity Inheritance to get this. So you have a base class which has all the fields mapped to ItemTable and then you have descendant classes that inherit from ItemTable entity and is mapped to the silo tables in the db. Every time you create a new silo you create a new entity mapped to that silo table.
[Table("ItemTable")]
public class Item
{
//All the fields in the table goes here
}
[Table("silo1_ItemTable")]
public class Silo1Item : Item
{
}
[Table("silo2_ItemTable")]
public class Silo2Item : Item
{
}
You can find more information on this here
Other option is to create a view that creates a union of all those table and map your entity to that view.
As mentioned in my comment, to solve this problem I am using the SQLQuery method that is exposed by DBSet. Since all my item tables have the exact same schema, I can use the SQLQuery to define my own query and I can pass in the name of the table to the query. Tested on my system and it is working well.
See this link for an explanation of running raw queries with entity framework:
EF raw query documentation
If anyone has a better way to solve my question, please leave a comment.
[UPDATE]
I agree that stored procedures are also a great option, but for some reason my management is very resistant to make any changes to our database. It is easier for me (and our customers) to put the sql in code and acknowledge the fact that there is raw sql. At least I can hide it from the other layers rather easily.
[/UPDATE]
Possible solution for this problem may be using context initialization with DbCompiledModel param:
var builder = new DbModelBuilder(DbModelBuilderVersion.V6_0);
builder.Configurations.Add(new EntityTypeConfiguration<EntityName>());
builder.Entity<EntityName>().ToTable("TableNameDefinedInRuntime");
var dynamicContext = new MyDbContext(builder.Build(context.Database.Connection).Compile());
For some reason in EF6 it fails on second table request, but mapping inside context looks correct on the moment of execution.

Entity Framework 5 SaveChanges Not Working, No Error

None of the many questions on this topic seem to match my situation. I have a large data model. In certain cases, only a few of the fields need be displayed on the UI, so for those I replaced the LINQ to Entity query that pulls in everything with an Entity SQL query retrieving only the columns needed, using a Type constructor so that I got an entity returned and not a DbDataRecord, like this:
SELECT VALUE MyModelNameSpace.INCIDENT(incident.FieldA, incident.FieldB, ...) FROM ... AS ...
This works and displays the fields in the UI. And if I make a change, the change makes it back to the entity model when I tab out of the UI element. But when I do a SaveChanges, the changes do not get persisted to the database. No errors show up in the Log. Now if I very carefully replace the above query with an Entity Sql query that retrieves the entire entity, like this:
SELECT VALUE incident FROM MyDB.INCIDENTs AS incident...
Changes do get persisted in the database! So as a test, I created another query like the first that named every column in the entity, which should be the exact equivalent of the second Entity SQL query. Yet it did not persist changes to the database either.
I've tried setting the MergeOption on the returned result to PreserveChanges, to start tracking, like this:
incidents.MergeOption = MergeOption.PreserveChanges;
But that has no effect. But really, if retrieving the entire entity with Entity Sql persists changes, what logical purpose would there be for behaving differently when a subset of the fields are retrieved? I'm wondering if this is a bug?
Gert was correct, the problem was that the entity was not attached. Dank U wel, Gert! Ik was ervan verbluft!
I just wanted to add a little detail to show the full solution. Basically, the ObjectContext has an Attach method, so you'd think that would be it. However, when your Entity SQL select statement names columns, and you create the object using a Type as I did, the EntityKey is not created, and ObjectContext.Attach fails. After trying and failing to insert the EntityKey I created myself, I stumbled across ObjectSet.Attach, added in Entity Framework 4. Instead of failing, it creates the EntityKey if it is missing. Nice touch.
The code was (this can probably be done in fewer steps, but I know this works):
var QueryString = "SELECT VALUE RunTimeUIDesigner.INCIDENT (incident.INCIDENT_NBR,incident.LOCATION,etc"
ObjectQuery<INCIDENT> incidents = orbcadDB.CreateQuery<INCIDENT>(QueryString);
incidents.MergeOption = MergeOption.PreserveChanges;
List<INCIDENT> retrievedIncidents = incidents.ToList<INCIDENT>();
orbcadDB.INCIDENTs.Attach(retrievedIncidents[0]);
iNCIDENTsViewSource.Source = retrievedIncidents;

Entity Framework Can't Load Related Entity

Ever since I started using POCO in my projects, I've been having problem querying data that references other entity on the query. The annoying part of it is that trying the same query on LINQPad works well.
For example, this esql query below:
SELECT VALUE TOP(1) a.AccountUrl FROM AppEntities.Accounts AS a WHERE EXISTS(SELECT VALUE u FROM a.Users AS u WHERE u.Username=#username)
throws the follow error when it tries to execute from my application.
Users' is not a member of type 'DelightModel.Account' in the currently loaded schemas. Near simple identifier, line 1, column 104.
I tried the same query on LINQPad with the same dll(Repository library) that my web application referenced, and it worked. Changing the query to return the full entity without projection (example below) also works with no problem.
SELECT VALUE TOP(1) a FROM AppEntities.Accounts AS a WHERE EXISTS(SELECT VALUE u FROM a.Users AS u WHERE u.Username=#username)
The above query work on my application.
Is this a bug or am I doing something wrong?
Please help point me towards the right direction. Thanks.

ADO.NET Entity Framework - How to select data from one Table only (and ignore other tables)?

Background is the team i'm in has just started using the EntityFramework; first we designed the database, put all the table relationships in place, foreign keys, etc; then thru visual studio add a new ADO.NET Entity Data Model, and auto-magically we get the generated edmx file representing the whole database !
Now i focus on two tables that provide data for all dropdowns and lookup lists;
TLookupDomain (domainID, domainName, domainDesc )
TLookup (lookupID, domainID, lookupCode, lookupDisplay, lookupDesc, sortOrder)
Relationship is 1-M going from left to right:
TLookupDomain -< TLookup -< TOther (+ another 30 or so other tables)
So lookupID is a foreign-Key to as many as 30 tables;
IQueryable<TLookup> qList = from l in ctx.TLookups
where l.domainID == 24
select l;
foreach (TLookup l in qList)
{
//do something.
System.Diagnostics.Debug.WriteLine("{0}\t{1}", l.lookupCode, l.lookupDisplay);
foreach (TOther f in l.TOthers)
{
System.Diagnostics.Debug.WriteLine("{0}\t{1}", f.feeAmount, f.feeDesc);
}
}
When i execute the above LINQ, i get all the fields for TLookup table (which is fair), BUT data is also fetched for the 30 or so tables that are linked to it, even though i am NOT interested in the other table's data at this point, and i am going to discard all data soon as LINQ fetches it.
Two Questions i have:
Q.1) Can i somehow modify the LINQ query above or tell the EntityFramework otherwise not to bother fetchin data from the 30 other linked tables ?
Q.2) is it "right" to have one edmx file that models the entire database? (sounds dodgy to me).
Configure Lazy Load to true for the model. Relations should be loaded only upon navegation. You can also split the models to avoid too many unnecessary relations.
Linq-to-Entities queries do not fetch anything automatically. Fetching of navigation properties is performet either by eager or lazy loading. You are not using eager loading because that requires calling Include in query (or ctx.LoadProperty separately). So if your data are fetched it must be due to lazy loading wich is enabled by default. Lazy loading triggers once you access the navigation property in the code.
You can also return only the data you need by using projections. Something like this should return readonly data:
var query = from l in ctx.TLookups
where l.domainId == 24
select new
{
l.lookupCode,
l.lookupDisplay,
l.TOthers
};
Having one or more EDMX is common dilemma. Working with single EDMX makes things more simple. If you want to know how to use multiple EDMXs and share conceptual definitions check these two articles: Part 1, Part 2.