entity-framework

Entity Framework Required Foreign Key = validation error on db.SaveChanges

霸气de小男生 提交于 2021-01-27 19:03:51
问题 I have these classes for my Company Entity public class Company { public Company() { this.Users = new HashSet<User>(); this.Tools = new HashSet<Tool>(); } public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public virtual ICollection<User> Users { get; set; } public virtual ICollection<Tool> Tools { get; set; } [Required] public virtual CompanyGroup CompanyGroup { get; set; } } public class CompanyDto { public string Name { get; set; } public

Filter in EF Include [duplicate]

余生长醉 提交于 2021-01-27 18:40:49
问题 This question already has answers here : EF: Include with where clause [duplicate] (5 answers) Closed 2 years ago . I have this LINQ query that gives an error on the filter in the Include. When searching my friend Google I've found that it's not possible to filter in an Include. I've found some ways to do this on an onther way but I can't get it to work for my specific case. return context.Timesheets.Where(t => t.UserId == userId && t.SubjectDate == date && t.WorkingDaySchedules.Count() > 0)

Comparing non nullable `int` to `null` (LINQ)

怎甘沉沦 提交于 2021-01-27 17:06:12
问题 I have EF model class and I've decided to extend that class with one bool property: class A { public int Id { get; set; } public string Value { get; set; } } class A_DTO : A { public bool BoolProp { get; set; } } class C { public int Id { get; set; } public int A_Id { get; set; } } Then I wrote a method, which will return that A collection joined with some other C collection, which contains A <=> C mapping (well, in real world example it contains some SystemId and linq query will be joined by

Entity Framework Error when using Unity

落花浮王杯 提交于 2021-01-27 16:31:24
问题 On trying to resolve my service class, I'm getting an error that DbContext cannot be constructed because it's an abstract class. The error message is here: Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'MyService.MyClass', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, System.Data.Common.DbConnection, is an abstract class and cannot be constructed. Are you missing a type mapping

C# Entity Framework error on async methods

↘锁芯ラ 提交于 2021-01-27 14:35:11
问题 I have already seen this, but I am experiencing another problem. I have this service class for managing ASP.NET identity roles: public class RoleService : IRoleService { private readonly RoleManager<ApplicationRole> _roleManager; public RoleService(RoleManager<ApplicationRole> roleManager) { this._roleManager = roleManager; } public async Task<IdentityResult> CreateAsync(ApplicationRole role) { return await this._roleManager.CreateAsync(role); } } As suggested by this question, I use the

The column name is not valid

喜欢而已 提交于 2021-01-27 13:42:59
问题 I get a common error that probably is easy to solve but I have no clue how. This is the message I get. The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if

Giving database context to object Factory

旧时模样 提交于 2021-01-27 13:21:50
问题 There is a question I always ask myself when I'm using a Factory pattern inside my code (C#, but it applies to any language I suppose). I have a "Service" that takes care of interacting with my database, do stuff with objects and interacts with my object model. This Service uses a Factory sometimes to delegate the instanciation of an object. But this factory obviously needs to interact by itself with the database to instanciate my object properly. Is it a good/bad practice to pass the

C# Entity Framework error on async methods

北慕城南 提交于 2021-01-27 13:17:02
问题 I have already seen this, but I am experiencing another problem. I have this service class for managing ASP.NET identity roles: public class RoleService : IRoleService { private readonly RoleManager<ApplicationRole> _roleManager; public RoleService(RoleManager<ApplicationRole> roleManager) { this._roleManager = roleManager; } public async Task<IdentityResult> CreateAsync(ApplicationRole role) { return await this._roleManager.CreateAsync(role); } } As suggested by this question, I use the

How to cast IQueryable<T> to DbSet<T>?

会有一股神秘感。 提交于 2021-01-27 13:02:41
问题 public DbSet<Item> Items { get { return dbContext.Item.Where(x => x.Id == id).Select(x=>x) } } The above code causes a compilation error: Cannot implicitly convert type 'System.Linq.IQueryable to ... DbSet. An explicit conversion exists (are you missing a cast?) After adding the explicit cast: public DbSet<Item> Items { get { return (DbSet<Item>)(dbContext.Item.Where(x => x.Id == id).Select(x => x)) } } a runtime error happens: Additional information: Unable to cast object of type 'Microsoft

How to cast IQueryable<T> to DbSet<T>?

北慕城南 提交于 2021-01-27 12:59:57
问题 public DbSet<Item> Items { get { return dbContext.Item.Where(x => x.Id == id).Select(x=>x) } } The above code causes a compilation error: Cannot implicitly convert type 'System.Linq.IQueryable to ... DbSet. An explicit conversion exists (are you missing a cast?) After adding the explicit cast: public DbSet<Item> Items { get { return (DbSet<Item>)(dbContext.Item.Where(x => x.Id == id).Select(x => x)) } } a runtime error happens: Additional information: Unable to cast object of type 'Microsoft