EF Core Unable to determine the relationship represented by navigation property 'Colour.ColourGroups' of type 'ICollection<ColourGroup>' - entity-framework-core

I have the following 3 tables in a legacy database.
CREATE TABLE Colour(
ColourCode char(3) NOT NULL,
CONSTRAINT PK_Colour PRIMARY KEY CLUSTERED
(
ColourCode ASC
)
CREATE TABLE ColourGroup(
ColourGroup varchar(6) NOT NULL,
CONSTRAINT PK_ColourGroup PRIMARY KEY CLUSTERED
(
ColourGroup ASC
)
CREATE TABLE Colour_ColourGroup(
ColourCode char(3) NOT NULL,
ColourGroup varchar(6) NOT NULL,
CONSTRAINT PK_Colour_ColourGroup PRIMARY KEY CLUSTERED
(
ColourCode ASC,
ColourGroup ASC
)
ALTER TABLE Colour_ColourGroup WITH CHECK ADD CONSTRAINT FK_Colour_ColourGroup_Colour FOREIGN KEY(ColourCode)
REFERENCES Colour (ColourCode)
ALTER TABLE Colour_ColourGroup WITH CHECK ADD CONSTRAINT FK_Colour_ColourGroup_ColourGroup FOREIGN KEY(ColourGroup)
REFERENCES ColourGroup (ColourGroup)
and the following EF classes to represent them in code
public class Colour
{
public string ColourId { get; set; }
public ICollection<ColourGroup> ColourGroups { get; set; }
}
public class ColourGroup
{
public string ColourGroupId { get; set; }
public ICollection<Colour> Colours { get; set; }
}
public class ColourColourGroup
{
public string ColourId { get; set; }
public string ColourGroupId { get; set; }
public Colour Colour { get; set; }
public ColourGroup ColourGroup { get; set; }
}
I'm using Fluent API to link ColourColourGroup to the Colour_ColourGroup table and to specify some DB column names.
builder.Entity<Colour>(entity =>
{
entity.Property(e => e.ColourId).HasColumnType("char(3)")
.HasColumnName("ColourCode");
entity.Property(e => e.FinishId).HasMaxLength(3);
entity.Property(e => e.Description).HasMaxLength(30);
entity.HasKey(e => e.ColourId);
});
builder.Entity<ColourGroup>(entity =>
{
entity.Property(e => e.ColourGroupId).HasMaxLength(6)
.HasColumnName("ColourGroup");
entity.HasKey(e => e.ColourGroupId);
});
builder.Entity<ColourColourGroup>(entity =>
{
entity.ToTable("Colour_ColourGroup");
entity.Property(e => e.ColourGroupId).HasMaxLength(6)
.HasColumnName("ColourGroup");
entity.Property(e => e.ColourId).HasMaxLength(3)
.HasColumnName("ColourCode");
entity.HasKey(e => new { e.ColourId, e.ColourGroupId });
});
But I don't know how to link the 3 tables together. If it was just a one-to-many relationship between Colour and ColourGroup, I think you'd use InverseProperty (or whatever the Fluent equivalent is) on ColourGroup.Colours.
Any query that accesses these entities fail with the above error, for example:
var group = _context.ColourGroups.Where(g => g.ColourGroupId == "ALLPC").FirstOrDefault();
How do I tell EF that the Colour.ColourGroups and ColourGroup.Colours properties are "mapped" by the corresponding properties in ColourColourGroup?

Related

EF7 - Custom column name for HasOne relationship

How do you specify a custom column name with a HasOne relationship in EF7?
Consider the following sample classes:
public class House
{
public int Id { get; set; }
public int BedroomCount { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string StreetName { get; set; }
public string StreetNumber { get; set; }
}
And this fluent configuration:
modelBuilder.Entity<House>()
.HasOne(x => x.Address)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
Which leads to this DB configuration:
CREATE TABLE [House] (
[Id] int NOT NULL IDENTITY,
[AddressId] int,
[BedroomCount] int NOT NULL,
CONSTRAINT [PK_House] PRIMARY KEY ([Id]),
CONSTRAINT [FK_House_Address_AddressId] FOREIGN KEY ([AddressId]) REFERENCES [Address] ([Id]) ON DELETE CASCADE);
CREATE TABLE [Address] (
[Id] int NOT NULL IDENTITY,
[StreetName] nvarchar(max),
[StreetNumber] nvarchar(max),
CONSTRAINT [PK_Address] PRIMARY KEY ([Id]));
How do I specify a column name other than "AddressId" on the House table? I cannot find a method similar to HasColumnName like there is on non-navigation properties.
I'm using Entity Framework 7 RC1-Final.
You can use Data Annotations to configure the foreign key of your relationship.
public int AddressID { get; set; }
[ForeignKey("AddressID")]
public Address Address { get; set; }
This requires a property that will be used as the foreign key in your relationship. Also, note that it is recommended that you have a explicit foreign key for your relationships other than a shadow foreign key. This will prevent you to have a lot of problems when inserting/updating since you don't need to set the entire navigation property Address to save a House entity. See the problem here
Not tested but perhaps this could work (can't find a way to install EF7 right now)
modelBuilder.Entity<House>()
.HasOne(x => x.Address)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
.HasForeignKey(x => x.AddressID);
You can check here for more examples: Foreign Key Relationships EF7
You can get property first from modelBuilder.Entity<House>().Metadata.GetProperties() list and then set its name
property.Relational().ColumnName = "YouCustomId"

Entity Framework Entity Splitting Into a single POCO

I'm using Entity Framework 6.1.3 and I want to define a single POCO class (Subscriber) which hydrates itself from three SQL tables. I have tried to follow various on-line references including this one from Deliveron. Here are the table/model definitions...
SQL Tables
CREATE TABLE [Subscriber]
(
[SubscriberId] INT NOT NULL PRIMARY KEY,
[Username] NVARCHAR(50) NOT NULL,
[Email] NVARCHAR(100) NOT NULL
)
CREATE TABLE [SubscriberDetails]
(
[SubscriberId] INT NOT NULL PRIMARY KEY,
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
CONSTRAINT [FK_SubscriberDetails_Subscriber]
FOREIGN KEY ([SubscriberId])
REFERENCES [Subscriber] ([SubscriberId]) ON DELETE CASCADE
)
CREATE TABLE [SubscriberProfile]
(
[SubscriberId] INT NOT NULL PRIMARY KEY,
[Bio] NVARCHAR(MAX) NULL,
CONSTRAINT [FK_SubscriberProfile_Subscriber]
FOREIGN KEY ([SubscriberId])
REFERENCES [Subscriber] ([SubscriberId]) ON DELETE CASCADE
)
Domain Model
public class Subscriber {
public int SubscriberId { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Bio { get; set; }
};
In my DataContext OnModelCreating() method, I have added the following code:
modelBuilder.Entity<Subscriber>()
.HasKey(m => m.SubscriberId)
.Map(m => {
m.ToTable("Subscriber");
m.Properties(s => new {
s.SubscriberId,
s.UserName,
s.Email
});
})
.Map(m => {
m.ToTable("SubscriberDetails");
m.Properties(s => new {
s.FirstName,
s.LastName
});
})
.Map(m => {
m.ToTable("SubscriberProfile");
m.Properties(s => new {
s.Bio
});
});
Whilst this compiles and loads, when I try and run a query against the DataContext I get the following error:
System.Data.SqlClient.SqlException: Invalid object name
'dbo.Subscriber21'
Can anyone give me any direction on what I am doing wrong?

Defining relationships with existing database in code first

I have an existing database with two tables: MemData and MemPhone.
MemData has the following structure
uidMemberId (PK, uniqueidentifier not null)
strFirstName (varchar(50), null)
strLastName (varchar(50), null)
MemPhone has the following structure
uidMemberId (uniqueidentifier not null)
strPhoneNumber (varchar(50), null)
There's no PK on the MemberPhoneNumber table. The two tables are joined by uidMemberId but no foreign keys have been set up in the database.
My MemData Model looks like this:
[Table("MemData")]
public class Member
{
[Key]
public Guid MemberId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual IList<MemberPhoneNumber> MemberPhoneNumbers { get; set; }
}
With the configuration map looking like this:
public MemberMap()
{
Property(t => t.MemberId).HasColumnName("uidMemberID");
Property(t => t.FirstName).HasColumnName("strFirstName");
Property(t => t.LastName).HasColumnName("strLastName");
HasMany(x => x.MemberPhoneNumbers);
}
My MemPhone Model looks like this:
[Table("MemPhone")]
public class MemberPhoneNumber
{
[Key]
public Guid MemberId { get; set; }
public string PhoneNumber { get; set; }
public virtual Member Member { get; set; }
}
With the configuration map looking like this:
public MemberPhoneNumberMap()
{
Property(t => t.MemberId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(t => t.MemberId).HasColumnName("uidMemberID");
Property(t => t.PhoneNumber).HasColumnName("strPhone");
}
In my MVC app, when I call
Uow.Members.GetAll().Include(p => p.MemberPhoneNumbers).ToList()
I get the following error
Invalid column name 'Member_MemberId'.
It should be looking for MemData_uidMemberId instead but I can't figure out what I've missed in the configuration.
Thanks
By default foreign key name will be propertyName _ entityId. That's why EF tries to map to Member_MemberId column. Provide foreign key configuration for one-to-many relationship
public class MemberMap : EntityTypeConfiguration<Member>
{
public MemberMap()
{
ToTable("MemData");
HasKey(m => m.MemberId);
Property(m => m.MemberId).HasColumnName("uidMemberID");
Property(m => m.FirstName).HasColumnName("strFirstName");
Property(m => m.LastName).HasColumnName("strLastName");
HasMany(x => x.MemberPhoneNumbers)
.WithRequired(p => p.Member)
.HasForeignKey(p => p.MemberId);
}
}
Also keep in mind, that you can configure key and table with fluent configuration, you don't need to pollute your entity with data annotations attributes.

Entity Framework Code First : how to annotate a foreign key for a "Default" value?

I have 2 classes: Client and Survey.
Each Client can have many surveys - but only one default survey.
I have defined the classes like this:
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string ClientName { get; set; }
public Nullable<int> DefaultSurveyID { get; set; }
[ForeignKey("DefaultSurveyID")]
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public class Survey
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string SurveyName { get; set; }
[Required]
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Client Client { get; set; }
}
This creates the Client table as I expect:
[dbo].[Clients]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[ClientName] [nvarchar](max) NULL,
[DefaultSurveyID] [int] NULL
)
But the Survey table has an extra foreign key:
[dbo].[Surveys]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[SurveyName] [nvarchar](max) NULL,
[ClientID] [int] NOT NULL,
[Client_ID] [int] NULL
)
Why is Code First generating this relationship and how to I tell it not to?
The problem is that when you have multiple relationships between two entities, EF Code First isn't able to find out which navigation properties match up, unless, you tell it how, here is the code:
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string ClientName { get; set; }
/****Change Nullable<int> by int?, looks better****/
public int? DefaultSurveyID { get; set; }
/****You need to add this attribute****/
[InverseProperty("ID")]
[ForeignKey("DefaultSurveyID")]
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
With your previous version, EF was creating that extra relationship because it didn't know that the DefaultSurvey property was referencing the ID of the Survey class, but you can let it know that, adding the attribute InverseProperty whose parameter is the name of the property in Survey you need DefaultSurvey to match with.
You can do it using code-first, but not being a code first expert I cheated :-)
1) I created the tables and relationships (as above without the extra Client_ID) in the database using SMS
2) I used Reverse Engineer Code First to create the required classes and mappings
3) I dropped the database and recreated it using context.Database.Create()
Original table defs:
CREATE TABLE [dbo].[Client](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[DefaultSurveyId] [int] NULL,
CONSTRAINT [PK_dbo.Client] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)
)
CREATE TABLE [dbo].[Survey](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[ClientId] [int] NULL,
CONSTRAINT [PK_dbo.Survey] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)
)
Plus foreign keys
ALTER TABLE [dbo].[Survey] WITH CHECK
ADD CONSTRAINT [FK_dbo.Survey_dbo.Client_ClientId] FOREIGN KEY([ClientId])
REFERENCES [dbo].[Client] ([Id])
ALTER TABLE [dbo].[Client] WITH CHECK
ADD CONSTRAINT [FK_dbo.Client_dbo.Survey_DefaultSurveyId]
FOREIGN KEY([DefaultSurveyId]) REFERENCES [dbo].[Survey] ([Id])
Code generated by reverse engineering:
public partial class Client
{
public Client()
{
this.Surveys = new List<Survey>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? DefaultSurveyId { get; set; }
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public partial class Survey
{
public Survey()
{
this.Clients = new List<Client>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? ClientId { get; set; }
public virtual ICollection<Client> Clients { get; set; }
public virtual Client Client { get; set; }
}
public class ClientMap : EntityTypeConfiguration<Client>
{
#region Constructors and Destructors
public ClientMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name).HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Client");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.DefaultSurveyId).HasColumnName("DefaultSurveyId");
// Relationships
this.HasOptional(t => t.DefaultSurvey)
.WithMany(t => t.Clients).HasForeignKey(d => d.DefaultSurveyId);
}
#endregion
}
public class SurveyMap : EntityTypeConfiguration<Survey>
{
#region Constructors and Destructors
public SurveyMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name).HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Survey");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.ClientId).HasColumnName("ClientId");
// Relationships
this.HasOptional(t => t.Client)
.WithMany(t => t.Surveys).HasForeignKey(d => d.ClientId);
}
#endregion
}
Entity Framework does exactly what it's told to do. What you've told it is that there is both a one-to-many and a one-to-one relationship between Clients and Surveys. It generated both FKs in the Survey table in order to map both of the relationships that you've requested. It has no idea that you're trying to connect the two relationships together, nor do I think does it have the ability to deal with that.
As an alternative you might want to consider adding a IsDefaultSurvey field on the Survey object so that you can query for the default survey through the Surveys collection that you have on the Client object. You could even go one step further and put it in as a NotMapped property on the Client object so that you could still use Client.DefaultSurvey to get the correct survey, and not have to change any of your other code, as follows:
[NotMapped]
public Survey DefaultSurvey
{
get { return this.Surveys.First(s => s.IsDefaultSurvey); }
}
Please notice that adding the code below you will fix the issue.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Client>()
.HasOptional(x => x.DefaultSurvey)
.WithMany(x => x.Surveys);
.HasForeignKey(p => p.DefaultSurveyID);
{
}

Many to many relation with the same table with EF4.3 Code First

How to make a configuration with this schema?
CREATE TABLE Entity
(
Id int identity primary key,
Name nvarchar(30)
)
CREATE TABLE Member
(
ParentEntityId references Entity(Id),
ChildEntityId references Entity(Id)
)
Like so:
Model class:
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Entity> Parents { get; set; }
public ICollection<Entity> Children { get; set; }
}
Mapping:
modelBuilder.Entity<Entity>()
.HasMany(e => e.Parents)
.WithMany(e => e.Children)
.Map(m =>
{
m.ToTable("Member");
m.MapLeftKey("ParentEntityId");
m.MapRightKey("ChildEntityId");
});