I am creating an Entity Framework 7 project to replace an Entity Framework 6 project.
I have an Item entity which belongs to a country. I then have a linq query that gets the count by country. Here is the query.
var results = allItems
.GroupBy(g => g.Country)
.ToDictionary(s => s.Key, s => s.Count());
This works with EF6 but throws an exception with EF 7 (the exception is at the bottom).
This is my entity:
public class Item
{
[Key]
public int id { get; set; }
[Required]
public int Countryid { get; set; }
[ForeignKey("Countryid")]
public virtual Country Country { get; set; }
}
In EF 7, with the debugger I see that the Country is null (that's the navigation property) but I do have the countryid. In EF 6, I have an object for the navigation property. In addition, I have unit tests using Moq and they work (the show the nav property).
I tried to add an include but I should not need that. I didn't need it in EF 6 or with the Mock.
Using include gives this:
var results = allItems
.Include(i => i.Country)
.GroupBy(g => g.Country)
.ToDictionary(s => s.Key, s => s.Count());
I get the same error.
Here is the error:
Expression of type
'System.Func2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier2[FMS.DAL.Entities.ActionItem,Microsoft.Data.Entity.Storage.ValueBuffer],Microsoft.Data.Entity.Storage.ValueBuffer],FMS.DAL.Entities.MemberCountry]'
cannot be used for parameter of type
'System.Func2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.MemberCountry]'
of method
'System.Collections.Generic.IEnumerable1[System.Linq.IGrouping2[FMS.DAL.Entities.MemberCountry,FMS.DAL.Entities.ActionItem]]
_GroupBy[ActionItem,MemberCountry,ActionItem](System.Collections.Generic.IEnumerable1[FMS.DAL.Entities.ActionItem],
System.Func2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.MemberCountry],
System.Func`2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.ActionItem])'
Currently GroupBy is not implemented in EF7 the status of features can be found on the road map page here. https://github.com/aspnet/EntityFramework/wiki/Roadmap
A work around would be:
context.Countries.Select( x => new
{
x.Id,
Items = x.Items.Count
} ).ToDictionary( x => x.Id, x => x.Items );
public class Country
{
public int Id { get; set; }
//Add this property
public virtual ICollection<Item> Items { get; set; }
}
//Generated SQL
SELECT (
SELECT COUNT(*)
FROM [Item] AS [i]
WHERE [x].[Id] = [i].[Countryid]
), [x].[Id]
FROM [Country] AS [x]
This would require adding a Items property to country but would allow you to achieve what you are after all in Linq. You could alse go for writing the query in sql and executing with EF but may not be the best option.
Related
I have two tables that join in a 1 to 1 relationship
using System.ComponentModel;
public class Product
{
public Product()
{
MaterialProperties = new MoreProductInfo
{
Product = this
};
}
[Key]
public int ItemId { get; set; }
[ForeignKey("ItemId")]
public virtual MoreProductInfo MaterialProperties { get; set; }
}
public class MoreProductInfo : IObjectSpaceLink, IElipseLookUp
{
[Key]
public int ItemID { get; set; }
[ForeignKey("ItemId")]
public virtual Product Product { get; set; }
}
The relationship is set up in FluentAPI like this:
modelBuilder.Entity<Product>()
.HasOne<MoreProductInfo>(x => x.MaterialProperties)
.WithOne(x => x.Product)
.HasForeignKey<MoreProductInfo>(m => m.ItemID);
When I try to save a new product I get
Microsoft.Data.SqlClient.SqlException
HResult=0x80131904
The column name 'ItemID' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code.
Source=Core Microsoft SqlClient Data Provider
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
I am using XAF 21.2.8 on .NET 6 and Entity Framework Core 5.0.14
I tried the following;
modelBuilder.Entity<Product>()
.HasOne(x => x.MaterialProperties)
.WithOne(x => x.Product)
.HasForeignKey<MoreProductInfo>(m => m.ItemID)
.HasForeignKey<Product>(x => x.ItemId)
.HasPrincipalKey<Product>(x => x.ItemId)
.HasPrincipalKey<MoreProductInfo>(x = >x.ItemID);
But this gives the error
System.InvalidOperationException
HResult=0x80131509
Message=The principal and dependent ends of the relationship cannot be inverted once foreign key or principal key properties have been specified.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.Metadata.Builders.InvertibleRelationshipBuilderBase..ctor(InternalForeignKeyBuilder builder, InvertibleRelationshipBuilderBase oldBuilder, Boolean inverted, Boolean foreignKeySet, Boolean principalKeySet, Boolean requiredSet)
I got it working by adjusting the attributes to use a different column name in one of the entities as follows
public class MoreProductInfo : IObjectSpaceLink, IElipseLookUp
{
[Column("ItemID")]
[Key] public int ExtItemID { get; set; }
[ForeignKey("ExtItemID")]
public virtual Product Product { get; set; }
// etc
and correcting the Fluent api
modelBuilder.Entity<Product>().HasOne(x => x.MaterialProperties).WithOne(x => x.Product)
.HasForeignKey<MoreProductInfo>(m => m.ExtItemID).HasPrincipalKey<Product>(x => x.ItemId);
This question already has an answer here:
What is the correct way to do many to many entity relation insert?
(1 answer)
Closed 11 months ago.
I have a many-to-many relationship established code-first that works, with thousands of fake records generated for an API. Now I'm trying to save a new record on one side of that relationship given only the ids of the other side, as the client is passing in an array of int ids.
I've found plenty of questions with problems and answers about saving many-to-many in general, but none specifically about doing so with just a list of foreign keys. Perhaps I'm simply using the wrong terminology?
I could grab all the records for those ids up front, but it seems very heavy to wait for a database query, assign those entities to the new entity, and then go to the database again to save, when all I really need is to establish a relationship with ids I already have.
For single relationships I would just add the foreign key as a separate property and set that instead of the foreign entity itself:
public int? CarId { get; set; }
[ForeignKey("CarId")]
public CarModel? Car { get; set; }
Is there perhaps a similar paradigm for many-to-many?
Entity setup:
public class ClownModel {
public int Id { get; set; }
public List<CarModel> Cars { get; set; }
}
public class CarModel {
public int Id { get; set; }
public List<ClownModel> Clowns { get; set; }
}
DB Context OnModelCreating:
builder.Entity<ClownModel>()
.HasMany(x => x.Cars)
.WithMan(x => x.Clows);
You can use a "stub entity" to add an existing Car to a new or existing Clown without fetching the Car. Eg
var newClown = new Clown();
var car = new Car() { Id = carId };
db.Entry(car).State = EntityState.Unchanged;
newClown.Cars.Add(car);
db.Set<Clown>().Add(newClown);
db.SaveChanges();
Or include the linking entity in your model, which you can do without adding a DbSet property or changing the Many-to-Many navigation properties.
eg
builder.Entity<Clown>()
.HasMany(x => x.Cars)
.WithMany(x => x.Clowns)
.UsingEntity<ClownCar>(
c => c.HasOne(x => x.Car)
.WithMany()
.HasForeignKey(x => x.CarId),
c => c.HasOne(c => c.Clown)
.WithMany()
.HasForeignKey(c => c.ClownId)
);
then
var newClown = new Clown();
var clownCar = new ClownCar();
clownCar.CarId = carId;
clownCar.Clown = newClown;
db.Set<ClownCar>().Add(clownCar);
db.SaveChanges();
I am trying to get a single User, with a list of Items, mapped with a many-to-many entity UserItems. However, I am unable to retrieve the mapped Items due to to an error that I'm unable to solve (error at bottom of question). Here is my code:
public class User
{
public int Id { get; set; }
public ICollection<UserItem> UserItems { get; set; }
}
public class Item
{
public int Id { get; set; }
public ICollection<UserItem> UserItems { get; set; }
}
public class UserItem
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public int ItemId { get; set; }
public Item Item { get; set; }
public int Quantity { get; set; }
}
The UserItem class configuration has the following relationships defined:
builder.HasOne(x => x.User)
.WithMany(x => x.UserItems)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.ClientCascade);
builder.HasOne(x => x.Item)
.WithMany(x => x.UserItems)
.HasForeignKey(x => x.ItemId)
.OnDelete(DeleteBehavior.ClientCascade);
I have the following generic repo with this method:
public class GenericRepository<T> : where T : class
{
private readonly DbContext _context;
public GenericRepository(DbContext context) => _context = context;
public T Get(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] navigationProperties)
{
IQueryable<T> query = _context.Set<T>();
query = navigationProperties.Aggregate(query, (current, property) => current.Include(property));
var entity = query.FirstOrDefault(where);
return entity;
}
}
However, when I try to run the code, I get an error on the Select(x => x.Item):
var user = repo.Get(x => x.Id == 1, x => x.UserItems.Select(y => y.Item));
Error:
System.InvalidOperationException: 'The expression 'x.UserItems.AsQueryable().Select(y => y.Item)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.'
What am I doing wrong, this seems to work for my other projects?
This error Occurs because you are not passing in a navigation property (x.UserItems would be a navigation property) but rather something you want to do with the navigation property. UserItems.Select(y => y.Item) is not a property of x because Select() is a function and therefore it cannot be included.
What you are trying to do (I assume it is including UserItems and also the corresponding Items) is not going to work with your current implementation of the repository. To include navigation properties of navigation properties .ThenInclude() must be used instead of .Include() which works only for navigation properties directly defined on the Entity the DbSet is created for.
But apart from your question I would suggest not to use such an generic implementation of Repository. The main benefit from using reposiories is to separarte code related to loading and storing of entities from the rest of your code. In your case if the consumer of repository knows that navigation properties must be included and that he has to provide them - then what is the point of having a repository at all? Then the consumer again cares about database specific code which makes having a repository unneccessary. I would recommend just making a conrete "UserRepository" which can only be used to retrieve users and explicitly includes the needed properties.
I need help getting my WebApi Controller to work.
I have a 3 table Models like this.
First Table
public class MainTable{
public int MainTableID { get; set; }
... Other Fields
public ICollection<SubTable> SubTables { get; set; }
}
Second Table
public class SubTable{
public int SubTableID { get; set; }
... Other Fields
public int MainTableID { get; set; }
[ForeignKey("MainTableID ")]
[JsonIgnore]
public virtual MainTable MainTable{ get; set; }
public ICollection<SubSubTable> SubSubTables { get; set; }
}
Third Table
public class SubSubTable{
public int SubSubTableID { get; set; }
... Other Fields
public int SubTableID { get; set; }
[ForeignKey("SubTableID")]
[JsonIgnore]
public virtual SubTable SubTable{ get; set; }
}
I need to flatten the first model because of other relationships not mentioned in this post so I am using a dto
DTO
public class TableDTO
{
public int MainTableID { get; set; }
... Other Fields (there is a lot of flattening happening here but I am going to skip it to keep this simple)
public ICollection<SubTable> SubTables { get; set; }
}
Now that I got all of that out of the way. To my question.. I am linking this all to a web api controller.
If I use the DTO and create a controller like this
Controller with DTO
public IQueryable<TableDTO> GetMainTable()
{
var mainTable = from b in db.MainTables
.Include(b => b.SubTable.Select(e => e.SubSubTable))
select new TableDTO()
{
MainTableID = b.MainTableID
eager mapping of all the fields,
SubTables = b.SubTables
};
return mainTable;
}
This works for everything except the SubSubTable which returns null. If I ditch the DTO and create a controller like this
Controller without DTO
public IQueryable<MainTable> GetMainTable()
{
return db.MainTables
.Include(c => c.SubTables)
.Include(c => c.SubTables.Select(b => b.SubSubTables));
}
This works perfect and the JSon returns everything I need, except that I lose the DTO which I desperately need for other aspects of my code. I have rewritten my code in every way I can think of but nothing works. I am pretty sure that this can be done with the DTO but I don't know what it would take to make it work, and as they say "You don't know what you don't know" so hopefully someone here knows.
In Entity Framework 6 (and lower), Include is always ignored when the query ends in a projection, because the Include path can't be applied to the end result. Stated differently, Include only works if it can be positioned at the very end of the LINQ statement. (EF-core is more versatile).
This doesn't help you, because you explicitly want to return DTOs. One way to achieve this is to do the projection after you materialize the entities into memory:
var mainTable = from b in db.MainTables
.Include(b => b.SubTable.Select(e => e.SubSubTable))
.AsEnumerable()
select new MessageDTO()
{
MainTableID = b.MainTableID ,
// eager mapping of all the fields,
SubTables = b.SubTables
};
The phrase, "eager mapping of all the fields" suggests that the projection isn't going to narrow down the SELECT clause anyway, so it won't make much of a difference.
Another way could be to load all SubSubTable objects into the context that you know will be in the MainTables you fetch from the database. EF will populate all SubTable.SubSubTables collections by relationship fixup.
If this works:
public IQueryable<MainTable> GetMainTable()
{
return db.MainTables
.Include(c => c.SubTables)
.Include(c => c.SubTables.Select(b => b.SubSubTables));
}
Then use this one and just add a Select() to the end with a ToList(). Note the IEnumerable in the return type:
public IEnumerable<MainTableDto> GetMainTable()
{
return db.MainTables
.Include(c => c.SubTables)
.Include(c => c.SubTables.Select(b => b.SubSubTables))
.Select(c=> new MainTableDto { SubTables=c.SubTables /* map your properties here */ })
.ToList();
}
Not sure about the types though (at one place you have MainTableDto, at another you mention MessageDto?).
I have the following table being created (by using DNX commands in EF7, now EF Core)
[Table("FishGrade")]
public partial class FishGrade
{
public FishGrade()
{
FishPrices = new HashSet<FishPrice>();
}
[HiddenInput]
[Column("FishGradeId")]
public int Id { get; set; }
[Column("GradeCode")]
[MaxLength(5), Required]
public string Code { get; set; }
[Column("GradeName")]
public string Name { get; set; }
public string IsActive { get; set; }
public virtual ICollection<FishPrice> FishPrices { get; set; }
}
But when it the table is created, the Code column (column named=GradeCode), is created as a 1 character long column.
I also have the following in the OnModelCreating method
modelBuilder.Entity<FishGrade>(entity =>
{
entity.Property(e => e.Code)
.HasColumnType("char");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.IsActive)
.HasMaxLength(1)
.HasColumnType("char");
});
Why is this happening? How can I get the column to be created with length = 5?
You can define your model in three ways:
conventions
attributes
fluent API
Conventions are applied first, then attributes, and finally the fluent API in your model builder. Your model builder is resetting the attribute configuration.
You should try to simplify your model configuration and use always the same method.
NOTE: take into account that, if you use something like MVC client side validation, it only understands the configuration made via attributes. In all other regards it doesn't mind how you configure your EF model.