Entity Framework “Update Model from Database” generates unexpected self-reference associations / navigation properties

EF4.1 Entity Framework “Update Model from Database” generates unexpected self-reference associations / navigation properties

Check in SQL server on the table design, if a relationship started to be formed, but was not completed then it may be left on the table. Removing this unnecessary relationship and then using “Update Model from Database” will remove these extra associations

Entity Framework: Error 111: Properties referred by the Principal Role…

The following error message may contain entity name or other project specific text

Error 111: Properties referred by the Principal Role must be exactly identical to the key of the EntityType referred to by the Principal Role in the relationship constraint for Relationship . Make sure all the key properties are specified in the Principal Role.

Solution upgarde to EF4.1 or higher.

 

Entity Connection String at run-time

 

Following is useful

Public Shared Function GetRunTimeEntityConnectionString(EntityConnectionStringName As String) As String
Dim currentConString = Configuration.ConfigurationManager.ConnectionStrings(EntityConnectionStringName).ConnectionString
Dim builder As New EntityClient.EntityConnectionStringBuilder(currentConString)
builder.ProviderConnectionString = <“NewConString”>
‘builder.Metadata = “res://*/LookupModel.csdl|res://*/LookupModel.ssdl|res://*/LookupModel.msl”
‘builder.Provider = “System.Data.SqlClient”
Return builder.ConnectionString
End Function

Usage
Dim EntityConnectionString = RuntimeConnectionString.GetRunTimeEntityConnectionString(<EntityConStringName>)
context = New <Namespace>.EntityModel.<EntityName>Entities(EntityConnectionString)

If trying to change actual SharedApp.config as follows then currently get an error.
Configuration.ConfigurationManager.ConnectionStrings(<“Name of Entity ConString”>).ConnectionString = RunTimeEntityConnectionString
Error:
system.configuration.configurationerrorsexception the configuration is read only

There are suggested methods for solving this online

LINQ and Entity Framework binding to ComboBox and DataGridViewComboBox

Private context As BC.SWT.EntityModel.SOPEntities

Private Sub LoadLookupData()

Try

    Dim EntityConnectionString = Configuration.ConfigurationManager.ConnectionStrings(“SOPEntities”).ConnectionString
    context = New BC.SWT.EntityModel.SOPEntities(EntityConnectionString)

    Dim CustomerQuery As ObjectQuery(Of Customer) = context.Customers
    ‘CustomerIDComboBox.DataSource = CustomerQuery.Execute(MergeOption.AppendOnly) ‘works but includes all columns
    Dim results = From a In CustomerQuery Select a.CustomerID, a.Customer1 Order By Customer1
    CustomerIDComboBox.DataSource = results
    CustomerIDComboBox.DisplayMember = “Customer1”

    CustomerIDDataGridViewComboBoxColumn.DataSource = results.ToList
    CustomerIDDataGridViewComboBoxColumn.DisplayMember = “Customer1”
    CustomerIDDataGridViewComboBoxColumn.ValueMember = “CustomerID”
    CustomerIDDataGridViewComboBoxColumn.DataPropertyName = “CustomerID”

Catch ex As Exception
    MessageBox.Show(ex.Message, Me.Text)
End Try

End Sub

LINQ samples

To use LINQ need

Imports System.Linq
May need reference to System.Core and System.Data
‘LINQ
Dim MRU As IEnumerable(Of PadMain) = From pm In mruList OrderBy pm.PadMaterial

LINQ to Entity Framework

Private
context As BC.SWT.EntityModel.SOPEntities

Dim EntityConnectionString = Configuration.ConfigurationManager.ConnectionStrings(“SOPEntities”).ConnectionString
context = New BC.SWT.EntityModel.SOPEntities(EntityConnectionString)

Dim
CustomerQuery AsObjectQuery(OfCustomer) = context.Customers
Dim results = From a In CustomerQuery Select a.CustomerID, a.Customer1 OrderBy Customer1

Entity Framework – Connection Strings

Technorati Tags:

See:

http://msdn.microsoft.com/en-us/library/vstudio/cc716756(v=vs.100).aspx

Entity Framework connection string for Entity Model

If in a different project in the same solution and a folder in that project then.


res://Project.ProjectName/Folder.ModelName.csdl|
res://Project.ProjectName/Folder.ModelName.ssdl|
res://Project.ProjectName/Folder.ModelName.msl;

If you struggle then the following will work, but may not be as efficient because the application has to search through available references. This is all that is needed though.


res://*/

 

  &lt;connectionStrings&gt;
    &lt;add name="MovieModel"
         connectionString="metadata=res://OneWay.EntityModel/Models.MovieModel.csdl|
                                    res://OneWay.EntityModel/Models.MovieModel.ssdl|
                                    res://OneWay.EntityModel/Models.MovieModel.msl;
                                    provider=System.Data.SqlClient;
                                    provider connection string=
                                      &amp;quot;data source=(local);
                                      initial catalog=DBName;
                                      integrated security=True;
                                      multipleactiveresultsets=True;
                                      App=EntityFramework;&amp;quot;"
         providerName="System.Data.EntityClient" /&gt;

    &lt;add name="OneWayEntitiesCF"
         connectionString="Data Source=(local);Initial Catalog=OneWay;Integrated Security=True"
         providerName="System.Data.SqlClient" /&gt;
  &lt;/connectionStrings&gt;

Error 1: System.IO.FileNotFoundException: Unable to resolve assembly ‘OneWay.EntityMode’.

Solution is to correct res://[this bit]/..

Error 2: System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.

Solution is to correct res://../[ThisBit]

Possibly you require FolderName.ModelName.

 

Other notes

If do not have Initial Catalog then will attach it to program and detach itself from SQL. Agh ! May also need User Instance = False for this.

Connection string in App.config.

Name and Model name need to match.

In edmx change Entity Set Names to have an ‘s’ on the end for each entity

Good luck !