I need to map following class on two tables -
public class Centre
{
public string CentreID { get; set;}
public string Name { get; set;}
public int Order { get; set;}
public string InfoText { get; set; }
public string Description { get; set; }
}
And the mapping used is
modelBuilder.Entity<Centre>()
.Map(m =>
{
m.Properties(t => new { t.CentreID, t.Name, t.Order });
m.ToTable("Centres");
})
.Map(m =>
{
m.Property(t => t.Description).HasColumnName("InfoText");
m.ToTable("CentreContents");
m.Requires("Attribute").HasValue("Description");
})
.Map(m =>
{
//m.Properties(t => new { t.InfoText });
m.Property(t => t.InfoText);
m.ToTable("CentreContents");
m.Requires("Attribute").HasValue("Intro");
});
The first 2 map works as expected. Adding 3rd map gives error. What expected is , there is a one to many relation and each record from linked table needs to be mapped on a property. The database is already existing in the application and not possible to change the structure. With view it is possible but don't want to create view for same.
Try removing the semi colon in this part of your statement...
});
.Map(m =>
Oh and detailing the error can be pretty helpful.
Related
I am trying to learn asp.NetCore 2.2. I am trying to setup a simple one page site. I have run into a problem with Automapper where manual Mappinng using forMember() is working at a top level for CreateMap<Listing, ListingSearchResultsDto>().ForMember(ListingPhotosUrl) but not at a lower level. I have another mapping CreateMap<User, UserDetailsDto>() where user contains an object Mylistings of type Listing. Mylistings is correctly auto mapped to ListingSearchResultsDto but manual configuration CreateMap<Listing, ListingSearchResultsDto>().ForMember(ListingPhotosUrl) is not applied.
I Have tried CreateMap<User, UserDetailsDto>().Formember(dest.Mylistings.ListingPhotosUrl,src.Mylistings.Photos.Url) but it seems that is not possible.
I Also tried this-> But no luck
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, UserDetailsDto>();
cfg.CreateMap<Listing, ListingSearchResultsDto>()
.ForMember(dest => dest.ListingPhotosUrl, opt =>
{
opt.MapFrom(src => src.Photos.FirstOrDefault(p => p.IsMain).Url);
});
});
var mapper = config.CreateMapper();
The Code:
AutoMappperProfiles
public AutoMapperProfiles()
{
CreateMap<Listing, ListingSearchResultsDto>()
.ForMember(dest => dest.ListingPhotosUrl, opt =>
{
opt.MapFrom(src => src.Photos.FirstOrDefault(p => p.IsMain).Url);
});
CreateMap<User, UserDetailsDto>();
CreateMap<ListingPhoto, ListingPhotosDetailedDto>();
}
User
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public ICollection<Listing> MyListings { get; set; }
}
UserDetailsDto
public class UserDetailsDto
{
public int Id { get; set; }
public string Username { get; set; }
public ICollection<ListingSearchResultsDto> MyListings { get; set;}
}
Listing
public int id { get; set; }
public string Category { get; set; }
public string Title { get; set; }
public ICollection<ListingPhoto> Photos { get; set; }
ListingSearchResultsDto
public class ListingSearchResultsDto
{
public int id { get; set; }
public string Title { get; set; }
public string ListingPhotosUrl { get; set; }
}
I am using CreateMap<Listing, ListingSearchResultsDto>().Formember(des,src) to manually map a destination property ListingPhotosUrl. I have another mapping CreateMap<User, UserDetailsDto>(). Inside User & UsedetailsDto classes i have a objects called MyListings of types ICollection<Listing> and ICollection<ListingSearchResultsDto> respectively. MyListings object is auto mapped correctly but ListingPhotosUrl manual mapping is not being applied. CreateMap<Listing,ListingSearchResultsDto>.Formember(des,src)) manual mapping is working at top level, but not at deeper level inside CreateMap<User, UserDetailsDto>(), is there anyway to fix this? thanks
FIXED - Automapper was working fine. Issue in Entity Framework DbContext. I did not include the photos as related data in the EF Core method for loading USER data GETUSER(). It was working with EF Core method for loading LISTING GetListing() because i had an include for photos Include(p => p.Photos).
After adding .ThenInclude(p => p.Photos) in GetUser(), the photos were returned with USER data and automapper successfully mapped User data and ListingPhotosUrl manual mapping was applied successfully.
Entity Framework Core DbContext:
public async Task<User> GetUser(int id)
{
var user = await _context.Users
.Include(a => a.Avatar)
.Include(l => l.MyListings)
.ThenInclude(p => p.Photos)
.FirstOrDefaultAsync(u => u.Id == id);
return user;
}
public async Task<Listing> GetListing(int id)
{
var listing = await _context.Listings
.Include(p => p.Photos)
.FirstOrDefaultAsync(l => l.id == id);
return listing;
}
I am using Entity Framework Core 2.2 to manage a SQL Server database of traded currencies. There are two entities in the model. The first is Currency, which specifies a trade-able currency, and the other is CurrencyPair, which specifies a pair of currencies that can be exchanged for one another.
public class Currency
{
public ulong Id { get; set; }
public string Name {get; set; }
[NotMapped]
public IEnumerable<CurrencyPair> Pairs
{
get { PairsAsBase?.Concat( PairsAsQuote ?? new CurrencyPair[0] ); }
}
public virtual IEnumerable<CurrencyPair> PairsAsBase { get; set; }
public virtual IEnumerable<CurrencyPair> PairsAsQuote { get; set; }
}
public class CurrencyPair
{
public ulong Id { get; set; }
public string Name { get; set; }
public ulong BaseCurrencyId { get; set; }
public ulong QuoteCurrencyId { get; set; }
public virtual Currency BaseCurrency { get; set; }
public virtual Currency QuoteCurrency { get; set; }
}
I would like to constrain the CurrencyPair table to disallow rows from having the same Currency for both BaseCurrency and QuoteCurrency fields. That is, if a specific currency has Id = 1, then a currency pair specifying BaseCurrencyId = 1 and QuoteCurrencyId = 1 would not be allowed.
Here is my DbContext.OnModelCreating implementation:
protected override void OnModelCreating( ModelBuilder modelBuilder )
{
modelBuilder.Entity<Currency>().HasKey(x => x.Id);
modelBuilder.Entity<Currency>().HasAlternateKey(x => x.Name);
modelBuilder.Entity<Currency>()
.HasMany(x => x.PairsAsBase)
.WithOne(x => x.BaseCurrency)
.HasForeignKey(x => x.BaseCurrencyId);
modelBuilder.Entity<Currency>()
.HasMany(x => x.PairsAsQuote)
.WithOne(x => x.QuoteCurrency)
.HasForeignKey(x => x.QuoteCurrencyId);
modelBuilder.Entity<CurrencyPair>().HasKey(x => x.Id);
modelBuilder.Entity<CurrencyPair>()
.HasOne(x => x.BaseCurrency)
.WithMany(x => x.PairsAsBase)
.HasForeignKey(x => x.BaseCurrencyId);
modelBuilder.Entity<CurrencyPair>()
.HasOne(x => x.QuoteCurrency)
.WithMany(x => x.PairsAsQuote)
.HasForeignKey(x => x.QuoteCurrencyId);
}
TL;DR: How can I ensure that two foreign key columns in a table do not both reference the same entity (using Entity Framework Core 2.2)?
AFAIK there is no good way to enforce your rule at the model builder lvl.
The next best thing would be to intercept SQL commands that could generate faulty data through an ef context, but the API isnt mature enough to make this an easy option.
In my opinion, the only options that you have left have no relation whatsoever to EF:
Constrain: Enforce the rule on your DB schema, eg. through a CHECK constraint
Validate: Enforce the rule at domain model level, eg. by intercepting the setters for both properties in the class and validating their values.
Did you try Global query filters, this should help you protect some unwanted to be show up when you query
modelBuilder.Entity<CurrencyPair>().HasQueryFilter(p => p.BaseCurrency != p.QuoteCurrency);
Data will still store in your table but it not show up when you using it.
I have recently converted my existing database to code using EF Reverse Engineer Code First. After polishing most of the results, I've started writing tests. The error I'm currently having is trying to access a column name called 'Element_ElementCode', however no such column exists.
To be sure of this, I've performed a search on my entire project to avoid the possibility of accidentally declaring it as such.
Find all "Element_ElementCode", Subfolders, Find Results 1, Entire Solution, ""
Matching lines: 0 Matching files: 0 Total files searched: 120
The exact error is as follows:
System.Data.SqlClient.SqlException: Invalid column name 'Element_ElementCode'.
Invalid column name 'Element_ElementCode' is repeated 10-15 times and the stacktrace doesn't provide any clue.
This exception occurs when executing a test which contains an expression to retrieve data and performs some asserts onto it.
var doos = (dbContext.Elementen.Where(d => d.ElementCode == "DOOS9001")).FirstOrDefault();
This is the result from a query in SQL Server itself:
Element (inside Element.cs) has the following fields:
ElementCode
Doelgroep
Type
Omschrijving
Titel
Elements are mapped like this:
public class ElementenMap : EntityTypeConfiguration<Element> {
public ElementenMap() {
// Primary Key
this.HasKey(t => t.ElementCode);
// Properties
this.Property(t => t.ElementCode)
.IsRequired()
.HasMaxLength(255);
this.Property(t => t.Type)
.HasMaxLength(31);
this.Property(t => t.Doelgroep)
.HasMaxLength(255);
this.Property(t => t.Omschrijving)
.HasMaxLength(255);
this.Property(t => t.Titel)
.HasMaxLength(255);
// Table & Column Mappings
this.ToTable("Elementen");
this.Property(t => t.ElementCode).HasColumnName("ElementCode");
this.Property(t => t.Type).HasColumnName("Type");
this.Property(t => t.Doelgroep).HasColumnName("Doelgroep");
this.Property(t => t.Omschrijving).HasColumnName("Omschrijving");
this.Property(t => t.Titel).HasColumnName("Titel");
// Relationships
this.HasMany(t => t.Kernwoorden)
.WithRequired(t => t.Element)
.Map(m => m.ToTable("Kernwoorden"));
}
}
Furthermore: I am certain the database is accessed because other tests on different tables inside the same database succeed.
I've tried to provide as much relevant info as possible, let me know if I've forgotten a source. Why is it trying to access a column called Element_ElementCode? Is it perhaps a convention I've forgotten to turn off (I've already had to turn off the PluralizingTableNameConvention)?
What direction should I be looking for this?
Edit:
Kernwoord.cs
public class Kernwoord {
public string ElementCode { get; set; }
public string KernwoordString { get; set; }
public virtual Element Element { get; set; }
}
Element.cs
public partial class Element {
public Element() {
this.LeertrajectElementen = new List<LeertrajectElement>();
this.Kernwoorden = new List<Kernwoord>();
}
public string ElementCode { get; set; }
public string Type { get; set; }
public string Doelgroep { get; set; }
public string Omschrijving { get; set; }
public string Titel { get; set; }
public virtual Casus Casus { get; set; }
public virtual Document Document { get; set; }
public virtual Doos Doos { get; set; }
public virtual ICollection<LeertrajectElement> LeertrajectElementen { get; set; }
public virtual StellingenSpel StellingenSpel { get; set; }
public virtual ICollection<Kernwoord> Kernwoorden { get; set; }
}
LeertrajectElementenMap.cs snippet:
this.HasRequired(t => t.Element)
.WithMany(t => t.LeertrajectElementen)
.HasForeignKey(d => d.ElementCode);
EDIT:
The problem was solved by fixing the existing inheritance issues.
You don't specify the foreign key column name of your relationship. It should be:
this.HasMany(t => t.Kernwoorden)
.WithRequired(t => t.Element)
.Map(m => m.MapKey("ElementCode")); // FK column name in Kernwoorden table
Or, if you have a foreign key property ElementCode in Kernwoorden class:
this.HasMany(t => t.Kernwoorden)
.WithRequired(t => t.Element)
.HasForeignKey(t => t.ElementCode);
There is a covention with respect to types.
For example if you have a customer and that customer has one and only one Adress, and the Adress has a Postcode.
Then it will generate a table Customer with a field Adress_Postcode.
In your case your Class name and Table name are different, so it is putting the type Element into table Elementen.
I am using Entity Framework 4.3.1 against a SQL Server 2012 database and I am using the POCO approach. I am getting the following error and I am wondering if anyone can explain how to fix it:
ModelValidationException
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
There is no InnerException available for any further information.
I cannot change the database schema and it is a little odd, but here it is...
** are the primary key (notice I have composite primary keys)
(FK) Denotes a foreign key
Here are the tables (if it helps I can post the SQL to generate them but I do not think the tables are actually the problem as the exception is in the validation of the model):
One
-
**OneId int not null
**TwoId int not null (FK)
**ThreeId int not null (FK)
Name nvarchar(50) not null
Two
-
**TwoId int not null
**ThreeId int not null (FK)
Name nvarchar(50) not null
Three
-
**ThreeId not null
Name nvarchar(50) not null
Here are the entities (notice that I am including the foreign keys in the model but other than that pretty standard):
public class Three
{
public int ThreeId { get; set; }
public string Name { get; set; }
public virtual ICollection<Two> Twos { get; private set; }
public virtual ICollection<One> Ones { get; private set; }
public void AddOne(One one)
{
if (one == null)
throw new ArgumentNullException("two");
if (Ones == null)
Ones = new List<One>();
if (!Ones.Contains(one))
Ones.Add(one);
one.Three = this;
}
public void AddTwo(Two two)
{
if (two == null)
throw new ArgumentNullException("two");
if (Twos == null)
Twos = new List<Two>();
if (!Twos.Contains(two))
Twos.Add(two);
two.Three = this;
}
}
public class Two
{
public int TwoId { get; set; }
public int ThreeId { get; set; }
public string Name { get; set; }
public virtual Three Three { get; set; }
public virtual ICollection<One> Ones { get; private set; }
public void AddOne(One one)
{
if (one == null)
throw new ArgumentNullException("two");
if (Ones == null)
Ones = new List<One>();
if (!Ones.Contains(one))
Ones.Add(one);
one.Two = this;
}
}
public class One
{
public int OneId { get; set; }
public int TwoId { get; set; }
public int ThreeId { get; set; }
public virtual Two Two { get; set; }
public virtual Three Three { get; set; }
}
And here is the data context:
public class DbCtx : DbContext
{
public DbCtx(string connectionString)
: base(connectionString)
{
Ones = Set<One>();
Twos = Set<Two>();
Threes = Set<Three>();
}
public DbSet<One> Ones { get; private set; }
public DbSet<Two> Twos { get; private set; }
public DbSet<Three> Threes { get; private set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var one = modelBuilder.Entity<One>();
one.ToTable("One");
one.HasKey(d => new
{
d.OneId,
d.TwoId,
d.ThreeId
});
one.Property(d => d.OneId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
one.HasRequired(t => t.Two)
.WithMany(s => s.Ones)
.HasForeignKey(t => t.TwoId);
one.HasRequired(t => t.Three)
.WithMany(s => s.Ones)
.HasForeignKey(t => t.ThreeId);
var two = modelBuilder.Entity<Two>();
two.ToTable("Two");
two.HasKey(d => new
{
d.TwoId,
d.ThreeId
});
two.Property(p => p.TwoId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
two.HasRequired(t => t.Three)
.WithMany(s => s.Twos)
.HasForeignKey(t => t.ThreeId);
var three = modelBuilder.Entity<Three>();
three.ToTable("Three");
three.HasKey(s => s.ThreeId);
three.Property(p => p.ThreeId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
base.OnModelCreating(modelBuilder);
}
}
Finally, this is a snippet of code to cause the exception:
using (var ctx = new DbCtx(#"....."))
{
Console.WriteLine(ctx.Twos.Count());
}
The reason for the error are incorrectly configured relations in your model. This is not correct:
one.HasRequired(t => t.Two)
.WithMany(s => s.Ones)
.HasForeignKey(t => t.TwoId);
one.HasRequired(t => t.Three)
.WithMany(s => s.Ones)
.HasForeignKey(t => t.ThreeId);
It should be:
one.HasRequired(t => t.Two)
.WithMany(s => s.Ones)
.HasForeignKey(t => new { t.TwoId, t.ThreeId });
Because dependent's FK must contain all columns of principal PK. You must also remove navigation property from Three to One.
This can also be caused by Code first from Database.
I had several views that I brought in that did not have an obvious key field according to Entity Framework conventions. The code generated put the [Key] attribute on the wrong field. In fact, it could not detect any uniqueness, so it put the [Key] attribute on all the fields.
I was able to remove all of the extra Key attributes to make the error go away.
Note for EF5+:
.HasForeignKey has been deprecated from EF 5: List of available methods (https://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.configuration.manytomanyassociationmappingconfiguration_methods(v=vs.103).aspx)
- MapLeftKey
- MapRightKey
- ToTable
If one were to need Many to Many where one 'Many' is to an Entity with a CompositeKey is:
one.HasKey(t => new { t.TwoId, t.ThreeId });
one.HasRequired(t => t.Two)
.WithMany(s => s.Ones)
.Map(m=>m.MapLeftKey("OneId").MapRIghtKey(new string[]{"TwoId", "ThreeId"}))
I have two tables in my database. One is called Users, and the other is called Widgets. The Widgets table represents 3 entities in my code model. One of the entities, Widget, is a parent class for the other two entities, WidgetTypeA and WidgetTypeB. Both WidgetTypeA and WidgetTypeB have navigation properties to the User entity, which is persisted to the Users table in the database. I'm having trouble getting Code First to use the same foreign key for both the WidgetTypeA and WidgetTypeB entities (UserId). Does anyone know how to do this? It seems like it should be a common problem with Table Per Hierarchy mapping.
My entity classes are as follows:
public class Widget
{
public int Id { get; set; }
public string Name { get; set; }
}
class WidgetMap : EntityTypeConfiguration<Widget>
{
public WidgetMap()
{
ToTable("Widgets");
HasKey(w => w.Id);
Property(w => w.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(w => w.Name)
.IsRequired()
.HasMaxLength(75)
.IsUnicode(true);
}
}
public class WidgetTypeA : Widget
{
public int UserId { get; set; }
public virtual User User { get; set; }
public string Color { get; set; }
public int DepthLevel { get; set; }
}
class WidgetTypeAMap : EntityTypeConfiguration<WidgetTypeA>
{
public WidgetTypeAMap()
{
Map(w => w.Requires("WidgetTypeId").HasValue(1));
HasRequired(w => w.User)
.WithMany(u => u.WidgetTypeAs)
.HasForeignKey(w => w.UserId)
.WillCascadeOnDelete(false);
Property(w => w.Color)
.IsOptional()
.IsUnicode(true)
.HasMaxLength(75);
Property(w => w.DepthLevel)
.IsOptional();
}
}
public class WidgetTypeB : Widget
{
public int UserId { get; set; }
public virtual User User { get; set; }
}
class WidgetTypeBMap : EntityTypeConfiguration<WidgetTypeB>
{
public WidgetTypeBMap()
{
Map(w => w.Requires("WidgetTypeId").HasValue(2));
HasRequired(w => w.User)
.WithMany(u => u.WidgetTypeBs)
.HasForeignKey(w => w.UserId)
.WillCascadeOnDelete(false);
}
}
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public int Age { get; set; }
public virtual ICollection<WidgetTypeA> WidgetTypeAs { get; set; }
public virtual ICollection<WidgetTypeB> WidgetTypeBs { get; set; }
}
class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
ToTable("Users");
HasKey(u => u.Id);
Property(u => u.Username)
.IsRequired()
.HasMaxLength(75)
.IsUnicode(true);
Property(u => u.Age)
.IsRequired();
}
}
At any rate, I keep getting the error
Invalid column name 'UserId1'
when I try to perform the following operations:
using (var entities = new MyEntities())
{
User u = new User
{
Username = "Frank",
Age = 14
};
entities.Users.Add(u);
entities.SaveChanges();
WidgetTypeA wa1 = new WidgetTypeA
{
Name = "0SDF81",
UserId = u.Id,
DepthLevel = 6
};
entities.WidgetTypeAs.Add(wa1);
entities.SaveChanges();
}
Not sure if this can be fixed or not. I can always specify a second UserId foreign key for the Widgets table, but that seems pointless. Perhaps there's a way to do this using Fluent API?
You cannot map properties defined in different derived entities to the same column. That is limitation in EF. If your WidgetTypeA has UserId property and your WidgetTypeB has UserId property they must be different columns in the database. It should work if you move both UserId and User properties from derived types to the parent Widget type.
I know its a long way late, but hopefully may help other readers.
Although Ladislav was correct that using a mapped Foreign Key is not supported in EF6, I did find a useful workaround.
It is possible to define a computed column specification whose expression simply refers to the original column. Userid in the description above. This can be used as the discriminator for the TPH mapping. With this approach, the column need not be persisted, but can be used for TPH, with the original column being available for use as a foreign key.