EF 6 query contains unmapped columns, breaking the query - entity-framework

I have an EDMX (Entity Framework 6.1.3) that I'm using to query two different databases. There are some minor differences between the databases but I only want the common columns. I generated the EDMX from Database A, and removed the columns that were not in Database B from the Diagram and regenerated the code.
If I query database B the query contains the columns I removed, although the final SELECT does not. This means that the query fails.
The table mapping shows the columns, but with nothing on the Value/Property side:
The exception is:
System.Data.Entity.Core.EntityCommandExecutionException : An error occurred while executing the command definition. See the inner exception for details.
----> System.Data.SqlClient.SqlException : Invalid column name 'ValidFromDate'.
Invalid column name 'ValidToDate'.
Invalid column name 'LastPulled'.
Invalid column name 'IsCurrent'.
The query that is being sent to the server is:
SELECT TOP (1)
[c].[FirstName] AS [FirstName],
[c].[LastName] AS [LastName],
[c].[HomePhone] AS [HomePhone],
[c].[WorkPhone] AS [WorkPhone],
[c].[MobilePhone] AS [MobilePhone],
[c].[Email] AS [Email],
[c].[Fax] AS [Fax]
FROM (SELECT
[Person].[FirstName] AS [FirstName],
[Person].[LastName] AS [LastName],
[Person].[HomePhone] AS [HomePhone],
[Person].[WorkPhone] AS [WorkPhone],
[Person].[MobilePhone] AS [MobilePhone],
[Person].[Email] AS [Email],
[Person].[Fax] AS [Fax],
[Person].[ValidFromDate] AS [ValidFromDate],
[Person].[ValidToDate] AS [ValidToDate],
[Person].[LastPulled] AS [LastPulled],
[Person].[IsCurrent] AS [IsCurrent]
FROM [dbo].[Person] AS [Person]) AS [c]
As you can see there is an inner-query which contains the additional columns.
At this point I'm kind of stumped as to why this is happening. How do I remove these columns from both sides of the mapping, or otherwise stop EF from putting unwanted columns in ANY part of the query?

When you use the EDMX designer and simply delete a column from an entity - this does not fully remove the column from the EDMX. Assuming you truly want it gone, you can open the EDMX file with a text editor and remove it by hand. To make sure that your manual changes trigger a rebuild of your auto-generated classes, edit it in Visual Studio and you shouldn't have an issue.
Right-click the EDMX in solution explorer
Open With...
XML (Text) Editor
I would expect if you were to open the EDMX, you would find something that looks like:
<EntityType Name="Person">
<Property Name="FirstName" Type="varchar" />
<Property Name="LastName" Type="varchar" />
<Property Name="HomePhone" Type="varchar" />
<Property Name="WorkPhone" Type="varchar" />
<Property Name="MobilePhone" Type="varchar" />
<Property Name="Email" Type="varchar" />
<Property Name="Fax" Type="varchar" />
<Property Name="ValidFromDate" Type="datetime" />
<Property Name="ValidToDate" Type="datetime" />
<Property Name="LastPulled" Type="datetime" />
<Property Name="IsCurrent" Type="short" />
</EntityType>
And you would just remove the bottom 4 columns, save and close, and rebuild the project.
EDIT: In case anyone in the future references this answer, if you do not first delete the column in the designer (like the OP did in this case), there will be two other instances of the column(s) in the XML that you would also need to remove in order for it to compile.

Thanks to #Borophyll for pointing me in roughly the right direction. Although their answer was not the solution to my issue, it did allow me to see the actual issue.
in the EDMX file there is also a entry that looks like this:
<EntitySet Name="Person" EntityType="Self.Person" store:Type="Tables" store:Schema="dbo">
<DefiningQuery>SELECT
[Person].[FirstName] AS [FirstName],
[Person].[LastName] AS [LastName],
[Person].[HomePhone] AS [HomePhone],
[Person].[WorkPhone] AS [WorkPhone],
[Person].[MobilePhone] AS [MobilePhone],
[Person].[Email] AS [Email],
[Person].[Fax] AS [Fax],
[Person].[ValidFromDate] AS [ValidFromDate],
[Person].[ValidToDate] AS [ValidToDate],
[Person].[LastPulled] AS [LastPulled],
[Person].[IsCurrent] AS [IsCurrent]
FROM [dbo].[Person] AS [Person]</DefiningQuery>
</EntitySet>
And that's where the weird sub-query was coming. I think the reason is that the table has no primary key as it is part of a staging database.
I just deleted from the SELECT statement the last four columns and everything worked.

Related

codefluent custom stored procedure

I have a custom stored procedure with in parameters that return fields of different tables how I can map this custom stored to an entity? I only want to use like a read only values for a report I don't want to save or something like that I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null
Solution 1: Using a view
A view allows to aggregate data from different entities.
<Article>
<Id />
<Name />
<Lines typeName="LineCollection" />
<cf:method name="LoadArticlesByCommand" body="load(string commandName) from ArticleByCommand where CommandName = #commandName" />
<cf:view name="ArticleByCommand" autoLightweight="true">
<ArticleName expression="Name"/>
<ArticleQty expression="Lines.Quantity" />
<CommandName expression="Lines.Command.Name" />
</cf:view>
</Article>
<Command>
<Id />
<Name />
<Lines typeName="LineCollection" />
</Command>
<Line setType="List">
<Article typeName="Article" key="true" />
<Command typeName="Command" key="true" />
<Quantity typeName="int" />
</Line>
http://blog.codefluententities.com/2014/04/22/views-auto-lightweight-and-the-modeler/
https://www.softfluent.com/documentation/Views_PersistentViews.html
Solution 2: Using a lightweight entity
Instead of creating a view, you can can create a lightweight entity that contains only the properties used by the stored procedure.
<cf:entity name="Person" lightweight="true">
<cf:property name="FirstName" typeName="string" />
<cf:property name="lastName" typeName="string" />
<cf:method name="ComputeBalance"
body="load () raw"
rawBody="SELECT 'John' AS FirstName, 'Doe' AS LastName" />
</cf:entity>
Solution 3: Custom mapping
For more specific values or types, a custom method can be provided to map the database values to .NET types. This custom method will be called with a DataReader as parameter, meaning that a developer could do whatever he wants.
<cf:entity name="Sample">
<cf:method name="LoadPair" body="raw" rawBody="SELECT 1234,5678"
returnTypeName="CodeFluent.Runtime.Utilities.Pair<System.Int32,System.Int32>"
cfom:methodName="On{0}" />
<cf:snippet>
private static CodeFluent.Runtime.Utilities.Pair<int,int> OnLoadPair(System.Data.IDataReader reader)
{
return new Pair<int, int>(reader.GetInt32(0), reader.GetInt32(1));
}
</cf:snippet>
</cf:entity>
You can also use OnAfterReadRecord or OnBeforeReadRecord rules
If it is not essential that you map the results of the custom stored procedure to an entity than another option is to use the built in support for DataSets.
http://blog.codefluententities.com/2011/06/22/dataset-support-in-codefluent-entities/
<cf:method name="LoadAllCities" body="raw" returnTypeName="System.Data.DataSet">
SELECT $Address::City$ FROM $Address$
</cf:method>
.
DataSet ds = Address.LoadAllCities();
foreach (DataTable table in ds.Tables)
{
foreach (DataRow row in table.Rows)
{
Console.WriteLine("City: " + row[0]);
}
}
Upon re-reading you're question I am providing another answer.
In response to the part where you said "I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null". The following steps should be able to solve that problem.
Execute one of the automatically created stored procedure in SQL Management Studio.
Execute the stored procedure you manually created.
Verify that the fieldnames returned by both stored procedures match.
I think the above will solve your immediate problem but I still don't like the solution. The reason is that you said you picked the most similar entity. I think that is going to cause problems in the future especially if the stored procedure is not being mapped to all of the entities properties.
I would recommend either lightweight entity, view or DataSet.

Entityframework Mapping Issue

I am using entity framework on mvc but I am having a problem with this method. All I am doing is a reflection method below and don't understand why I am getting a field mapping error.
I also get the following error on the fields mentioned here.
Error :-
Error 13 Error 3021: Problem in mapping fragments starting at line 205:Each of the following
columns in table FormBuilder_Form_Fields is mapped to multiple conceptual side properties:
FormBuilder_Form_Fields.ID is mapped to <FormFieldsForm.Form.ID, FormFieldsForm.FormFields.ID>
C:\NewDevelopment\CaseddimensionsCMS\CaseddimensionsCMS\CaseddimensionsCms.edmx 206 11 CaseddimensionsCMS
Error 14 Error 3021: Problem in mapping fragments starting at line 228:Each of the following columns in table FormBuilder_field_values is mapped to multiple conceptual side properties:
FormBuilder_field_values.ID is mapped to <FormFieldValues.FieldValues.ID, FormFieldValues.Form.ID>
I am not to sure what this means as quite new to entity framework.
I have included a screen shot of the edmx file in the layout designer:
This is a pastbin of my edmx file
http://pastebin.com/GeL6mZd4
As to long a code didnt want to be posting it here.
Having the same issue I found the solution here.
In short, you should:
Fixing this duplicate mapping issue requires a referential constraint,
which the designer will only support in the next release, so save the
edmx file, close it, then right-click it in Solution Explorer, select
“Open With…” and double click on “XML Editor”.
In the CSDL section, you will see the ProductProductImages
association:
Update your associations like:
<Association Name="FormsFormsFields">
<End Type="TableSplittingModel.Forms" Role="Form" Multiplicity="1" />
<End Type="TableSplittingModel.FormFields" Role="FormFields" Multiplicity="1" />
</Association>
by adding a ReferentialConstraint
<Association Name="FormsFormFields">
<End Type="TableSplittingModel.Forms" Role="Forms" Multiplicity="1" />
<End Type="TableSplittingModel.FormFields" Role="FormFields" Multiplicity="1" />
<ReferentialConstraint>
<Principal Role="Forms"><PropertyRef Name="id"/></Principal>
<Dependent Role="FormFields"><PropertyRef Name="id"/></Dependent>
</ReferentialConstraint>
</Association>

The changes to the database were committed successfully, but an error occurred while updating the object - revised

I am using EF in .NET V4.0 in Visual Basic (VS2010) with SQL Compact Edition 4.0. We are building a set of simple forms to maintain some tables. One table 'Companies' is linked to 2 other tables (People,CalibrationInfo) with Companies as the parent table. The Entity Type Definition is:
<EntityType Name="Company">
<Documentation>
<Summary>Provides a list of Companies and shipping addresses.</Summary>
</Documentation>
<Key>
<PropertyRef Name="CompanyID" />
</Key>
<Property Name="CompanyID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="String" MaxLength="100" Unicode="true" FixedLength="false" Nullable="false" />
<Property Name="Address1" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="Address2" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="Address3" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="Telephone" Type="String" MaxLength="30" FixedLength="false" Unicode="true" />
<Property Name="PrimaryContactID" Type="Int32" a:GetterAccess="Public" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" a:SetterAccess="Public" Nullable="true" >
<Documentation>
<Summary>Optional Primary Contact ID for the primary contact for this company.</Summary>
</Documentation>
</Property>
<Property Name="Disabled" Type="Boolean" Nullable="false" DefaultValue="false" />
<NavigationProperty Name="Calibrations" Relationship="NWCUDataStoreModel.FK_CalibrationInfo_Company" FromRole="Companies" ToRole="CalibrationInfo" />
<NavigationProperty Name="PrimaryContact" Relationship="NWCUDataStoreModel.FK_Company_PrimaryContact" FromRole="Company" ToRole="Person" />
</EntityType>
The form uses a binding source set to the Company set in the context:
bsCompanies = ctx.Companies.OrderBy("it.Name")
The Binding Source is linked to a Navigation Bar. Pressing the BindingNavigatorAddNewItem button gets a new record created. I enter only the company name tab to the next field and press the Save button. The link to the Primary Contact is set to nothing so there are no other relationships for this record. The Save button executes the following:
RowsSaved = ctx.SaveChanges()
This generates the InvalidOperationException. The Inner exception is:
AcceptChanges cannot continue because the object's key values conflict with another object...
There are no other records in the database with the name set to 'Test'. The exception indicates that the record was saved, but was unable to accept the changes. The record is still marked as Added. Calling ctx.AcceptChanges after this error generates an exception.
If I were doing this directly in code, instead of with BindingSource on a form, it would essentially look like this:
dim company as New Company
company.Name="Test"
company.PrimaryContactID = nothing
ctx.Companies.Add(company)
ctx.Save
I have looked at other examples of this on multipe sites, and have applied any fixes I could find, including setting the PrimaryContact id directly to a the correct Person record ID and setting the PrimaryContact to Nothing. Nothing makes any difference.
I have also deleted the three tables from the model and then reloaded them. No difference.
I have used this same code with no problems in SQL Server, but almost nothing seems to work with SQL Compact edition V4.0. You would think it should not be so difficult to store a single record into a table. If we have to go back to data sets, I have a lot of recoding to do.
Any suggestions or insights appreciated.
Thanks, Neil
BTW, the answer to this was to use Nuget to download Entity Framework 6.0, Entity Framework SQL Server Compact and Microsoft SQL Server Compact 4.0. I then downloaded the EF Framework 5 DB Generator .tt file as the EF 6 version does not work in Visual Studio 2010. You do this from the Add Code Generation menu item, and select Online Templates->EF 5 DBContext Generator... Finally, I modified the file using this Microsoft article: Databinding with WinForms. After that, things started to work. EF 4.0 does not work with SQL Server Compact, out of the box, without the changes described above. Using the ObservableListSource class described in the article also helped with parent-child relationships on forms, which did not work until I switched to this class.

Reset ADO.NET schema

I modified a schema (set a field to be non-nullable), but when I try to recreate the mapping with ADO.NET I only see the old schema.
The .edmx file looks like this:
<EntityType Name="STG_DW_BUF_CODE_D">
<Key>
<PropertyRef Name="BUF_CODE_KEY" />
</Key>
<Property Name="BUF_CODE_KEY" Type="number" Nullable="false" />
…
<EntityType Name="STG_DW_REGION_D">
<Property Name="REGION_KEY" Type="number" />
The STG_DW_REGION_D view should have Nullable="false" like the view above it.
I can confirm the new schema has this field non-nullable through another SQL application, but I can't get ADO.NET to notice.
I tried erasing the model and recreating it. I tried closing visual studio and starting it up again. It still sees the old schema.
Does anyone know how to reset it? Any suggestions?
This is either a bug in ADO.NET or ODP (Oracle's connection to Linq). If you add a field it will remove the cached schema and pull in a new schema with updated field attributes.

In Entity Framework, getting the value of an identity column after inserting

I'm using EF4. I want to insert a new MyObject into the database. MyObject has two fields:
Id: int (Identity) and
Name: string
As I've seen in documentation Entity Framework is supposed to set MyObject.Id to the value generated by database after the call to SaveChanges() but in my case that doesn't happen.
using (var context = new MyEntities())
{
var myObject = MyObjects.CreateMyObject(0, "something"); // The first parameter is identity "Id"
context.MyObjects.AddObject(myObject);
context.SaveChanges();
return myObject.Id; // The returned value is 0
}
UPDATE:
This happens in one of my entities and others work fine. By the way, I checked and the DB column is identity and StoreGeneratedPattern is set to Identity.
Here is the SSDL. I don't see any difference. The first one isn't working right:
<EntityType Name="OrgUnit">
<Key>
<PropertyRef Name="Srl" />
</Key>
<Property Name="Srl" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="TypeId" Type="smallint" Nullable="false" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="80" />
</EntityType>
<EntityType Name="OrgType">
<Key>
<PropertyRef Name="Srl" />
</Key>
<Property Name="Srl" Type="smallint" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Title" Type="varchar" Nullable="false" MaxLength="120" />
<Property Name="Options" Type="int" Nullable="false" />
</EntityType>
The update is done successfully in the database and the identity is generated but the entity object is not updated with the new identity.
In that case, you EF model is probably not up to date - EF should automagically get your new ID from the database. Try refreshing your EF model.
Your identity column's properties should look like this in your EDMX model:
If you're using Oracle Entity Framework 4 Provider, like I do, from ODP.NET, there is a bug in Designer. Just selecting the Identity value in drop down box will not do. It will annotate the conceptual property in conceptual model with
annotation:StoreGeneratedPattern="Identity"
like in
<Property Type="Int32" Name="Id" Nullable="false" cg:SetterAccess="Private" annotation:StoreGeneratedPattern="Identity" />
But, it will fail to do the same for Storage Model, ie. you need to do it manually. Find the Property (in my case ID) in EntityType of interest and add StoreGeneratedPattern="Identity".
<EntityType Name="PROBLEMI">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="number" Nullable="false" Precision="10" StoreGeneratedPattern="Identity" />
I'm not aware of the same bug in SQL EF provider 'cos I didn't use it.
This should "just work." Make sure the DB column actually is IDENTITY, and that StoreGeneratedPattern is set to Identity in EDMX.
wow! that was a nightmare but at last I solved it, although I didn't understand what the problem was. Maybe this helps someone with the same problem.
Generate the script for creating the table and its data.
Drop the table.
Run the script.
If you are using Linq To Entities, and get this error even if you followed the advices of marc_s (that are really good), you should look at your entites directly in the edmx (xml view) and check if they have the following attribute :
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="pk" />
</Key>
<Property Name="pk" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="value" Type="float" Nullable="false" />
</EntityType>
The StoreGeneratedPattern="Identity" is also required.
Try using refresh method after Save Changes, it has been documented in MSDN
"To ensure that objects on the client have been updated by data source-side logic, you can call the Refresh method with the StoreWins value after you call SaveChanges."
http://msdn.microsoft.com/en-us/library/bb336792.aspx
Though i feel what #Craig has suggested might also work.
I ran into this today. The difference though was I was using an insert function, where the above person doesn't specify that. What I had to do was make my insert function stored procedure return SCOPE_IDENTITY() and use a result binding for the id returned.
Fixed my issue.