entity-framework-core

.Net Core 2 - EF Core Error handling Save changes

若如初见. 提交于 2019-11-30 11:38:29
I'm used with EF6 and my Base repository Save() looks like this public void Save() { try { Context.SaveChanges(); } catch (DbEntityValidationException ex) { //Do Stuff } catch (Exception exception) { //Do stuff. } else { throw; } } DbEntityValidationException is an expected error from EF if the object save is invalid. Now that I'm on a new .NET Core 2 project and I need to know what is the Expected entity validation error type in EF Core. Looking through the Github issues, there is no DbEntityValidationException equivalent in Entity Framework Core. There's a blog (linked from issue #9662 on

Injecting Env Conn String into .NET Core 2.0 w/EF Core DbContext in different class lib than Startup prj & implementing IDesignTimeDbContextFactory

会有一股神秘感。 提交于 2019-11-30 11:10:22
I honestly cannot believe how hard this is...first off the requirements that I am going for: Implementing Entity Framework Core 2.0' IDesignTimeDbContextFactory which is IDbContextFactory renamed to be less confusing to developers as to what it does I do not want to have to do loading of appsettings.json more than once. One reason is because my migrations are running in the domain of MyClassLibrary.Data and there is no appsettings.js file in that class library, I would have to to Copy to Output Directory appsettings.js . Another reason is that it just not very elegant. So here is what I have

Configuring Dbcontext as Transient

落爺英雄遲暮 提交于 2019-11-30 11:02:30
In ASP.NET Core / EntityFramework Core, the services.AddDbContext<> method will add the specified context as a scoped service. It's my understanding that that is the suggested lifetime management for the dbcontext by Microsoft. However, there is much debate in our engineer department over this and many feel that the context needs to be disposed of ASAP. So, what is the best way to configure the dbcontext as Transient that still maintains the same Repository pattern typically used (i.e. injecting the context directly into the repository's constructor) as well as supporting flexible unit testing

How to use inherited properties in EF Core expressions?

六月ゝ 毕业季﹏ 提交于 2019-11-30 09:23:40
问题 We need construct expression for EF in dynamic way. For example create test models: public class TestBase { public int Id { get; set; } } public class TestCard : TestBase { public Guid Guid { get; set; } } Create a linq query: var q = from card in Context.hlt_disp_Card select new TestCard { Id = card.disp_CardID, Guid = card.Guid }; Normal using expressions: Expression<Func<TestCard, bool>> ex1 = card => card.Id == 1030; q.Where(ex1).ToList(); We require create expression from any type and

ef-core load collection property of nested tph inherited member

久未见 提交于 2019-11-30 08:59:12
Given the following class structure public class Parent { public Guid Id { get; public List<BaseChild> Children { get; set; } } public abstract class BaseChild { public int Id { get; set; } public string ChildName { get; set; } } public class NormalChild : BaseChild { public DateTime BirthDate { get; set; } } public class RichChild : BaseChild { public List<OffshoreAccount> OffshoreAccounts { get; set; } } public class OffshoreAccount { public string AccountNumber { get; set; } public AccountInfo AccountInfo { get; set; } } What is the best way to query parent data to include information about

How to dynamically order by certain entity properties in Entity Framework 7 (Core)

萝らか妹 提交于 2019-11-30 08:45:18
问题 I have a project where the front-end JavaScript specifies a list of columns to order by. Then in the back-end I have multi-layer application. Typical scenario Service layer (the service models' (DTO) properties match whatever the client-side wants to order by) Domain layer (it exposes repository interfaces to access persisted objects) ORM layer (it implements the repository and it uses Entity Framework 7 (a.k.a Entity Framework Core) to access a SQL Server database) Please note that System

How to make lazy-loading work with EF Core 2.1.0 and proxies

岁酱吖の 提交于 2019-11-30 08:39:55
I have the following models: public class Session { public int SessionID { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } public class User { public int UserID { get; set; } public int OrganizationID { get; set; } public virtual ICollection<Session> Sessions { get; set; } public virtual Organization Organization { get; set; } } public class Organization { public int OrganizationID { get; set; } public virtual ICollection<User> Users { get; set; } } that are registered in DbContext as: modelBuilder.Entity<Session>(entity => { entity.ToTable("sessions");

Update entity class in ASP.NET Core Entity Framework

本秂侑毒 提交于 2019-11-30 08:22:42
I have created the model from existing database using Entity Framework in ASP.NET Core. Here is the model of the Market table public partial class Market { public Guid MarketId { get; set; } public string City { get; set; } public string CityF { get; set; } public string Name { get; set; } public string NameF { get; set; } public int SortOrder { get; set; } public bool IsActive { get; set; } } However, I have change the MarketId datatype to int in the database. Now I want to update the model. Found some link but this link create the entire model again https://docs.microsoft.com/en-us/ef/core

Entity Framework Core - Lazy Loading

旧时模样 提交于 2019-11-30 08:07:35
Bowing to my Visual Studios request, I started my latest project using Entity Framework Core (1.0.1) So writing my database models as I always have using the 'virtual' specifier to enable lazy loading for a List. Though when loading the parent table it appears that the child list never loads. Parent Model public class Events { [Key] public int EventID { get; set; } public string EventName { get; set; } public virtual List<EventInclusions> EventInclusions { get; set; } } Child Model public class EventInclusions { [Key] public int EventIncSubID { get; set; } public string InclusionName { get;

EF7 RC1 : Disable Cascade Delete

…衆ロ難τιáo~ 提交于 2019-11-30 08:03:51
In the RC1 of EntityFramework 7, released yesterday, Cascade Delete was added. To disable it per relationship, I can use : builder.Entity<Site>().HasOne(e => e.Person) .WithMany(x => x.Sites).Metadata.DeleteBehavior = DeleteBehavior.Restrict; I want to disable it globally for a DbContext, but I didn't find a way. How can I do ? Someone stated on the github project forum that the only way to do it right now is to iterate through all relationships in the method OnModelCreating(ModelBuilder builder) , and set the DeleteBehavior property to DeleteBehavior.Restrict : foreach (var relationship in