ef-core-2.0

Enabling Migrations in EF core?

扶醉桌前 提交于 2020-01-02 05:32:11
问题 I'm getting started with EF Core 2.0, I have a console application targetting .NET 4.6.1 I have a very simple model class, and this context: public class ContextCore : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["efCoreCon"].ConnectionString); } public DbSet<ModelC> Models { get; set; } } this is the connection string: <add name="efCoreCon" connectionString="server=PC-MSHWF

EF Core 2.0 migration - Many-to-Many with additional fields

别等时光非礼了梦想. 提交于 2020-01-01 17:13:31
问题 I'm using EF Core 2.0 and created a many-to-many relationship with a join entity. When I add a new migration EF always creates an additional Index/Id-field which is completely stupid. Here is my join entity: public class Team_Member { public int TeamId { get; set; } public Team Team { get; set; } public int MemberId { get; set; } public Member Member { get; set; } public MemberTypeEnum MemberType { get; set; } } This is the configuration of the join table (following several examples on the

Which one must I use, MyDbContext.Blogs.Add(ablog) or MyDbContext.Add(ablog)?

♀尐吖头ヾ 提交于 2019-12-31 04:23:06
问题 Consider I have a context MyDbContext inherits DbContext of EFCore 2.0. Blogs is a DbSet<Blog> and Blog is an entity model. When I add a new Blog instance, ablog to the Blogs , which one must I use? MyDbContext.Add(ablog); or MyDbContext.Blogs.Add(ablog); ? How about Find ? MyDbContext.Find<Blog>(1); or MyDbContext.Blogs.Find(1); ? Is there any benefit to use one over the other one? 回答1: Adding directly data via the DbContext is new to the DbContext in Entity Framework Core and have no

How to make dynamic inclusion of navigation properties?

女生的网名这么多〃 提交于 2019-12-30 14:56:54
问题 I've a little problem. Assuming a Entity like this public class FirstEntity { public int ID { get; set; } public string Prop1 { get; set; } public string Prop2 { get; set; } public virtual ICollection<SecondEntity> SecondECollection { get; set; } } public class SecondEntity { public int ID { get; set; } public string Prop1 { get; set; } public virtual ThirdEntity Third { get; set; } } In repository, to get the entity with all the navigation properties i've to do something like this public

How to create user defined functions using code first in ABP?

我的未来我决定 提交于 2019-12-24 11:29:49
问题 I want to create some user-defined functions using code first approach in abp framework. I have created stored procedures xyz by adding xyz.sql inside EntityFrameworkCore\Migrations\Stored Procedures folder, But not able to find a way to create a function, please help. 来源: https://stackoverflow.com/questions/48262776/how-to-create-user-defined-functions-using-code-first-in-abp

Using EF Core to invoke stored procedure and closing connection

依然范特西╮ 提交于 2019-12-24 07:39:09
问题 I have an ASP.NET Core 2.2 application using EF Core. I have service class which typically use a DbContext for any CRUD operations. However in one of the method ( Assign method below) I need to use stored procedure. So I am using the following code. Note that DbContext is injected as Scoped instance. public class MyService : IMyService { private readonly MyDbContext _dbContext; public MyService(MyDbContext dbContext) { _dbContext = dbContext; } public async Task<Employee> GetByID(int id) {

Rename a foreign key in Entity Framework Core without dropping data

北城以北 提交于 2019-12-21 02:35:35
问题 I have two model classes: public class Survey { public int SurveyId { get; set; } public string Name { get; set; } } public class User { public int UserId { get; set; } public int SurveyId { get; set; } public Survey Survey { get; set; } } I want to rename Survey to StudentSurvey , so it will have StudentSurveyId . I update the class name and the properties in the models accordingly and add a migration. However, I get: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK

How to remove the role-related tables from ASP.NET Identity Core 2.0

旧巷老猫 提交于 2019-12-20 02:52:13
问题 With the advice read-elsewhere that Roles are a subset of Claims, I am looking at a clean way to ask the EF Core implementation in ASP.NET Identity not to create role-related tables in the ASP.NET Identity Core 2.0 template in VS 2017. Only claims are needed. The template uses public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating

How can I stop EF Core from creating a filtered index on a nullable column

荒凉一梦 提交于 2019-12-17 16:44:30
问题 I have this model: public class Subject { public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } public int LevelId { get; set; } [ForeignKey("LevelId")] public Level Level { get; set; } [Column(TypeName = "datetime2")] public DateTime? DeletedAt { get; set; } } And index configured via Fluent API: entityBuilder.HasIndex(e => new { e.LevelId, e.Name, e.DeletedAt }) .IsUnique(); It's creating a table with a unique filtered index. How can I prevent EF from

EF Core: Soft delete with shadow properties and query filters

此生再无相见时 提交于 2019-12-17 16:13:43
问题 I've created an interface to try to do a soft delete, mixing shadow properties and query filters. But it's not working. public interface IDeletableEntity {} And then in my model builder builder.Model.GetEntityTypes() .Where(entityType => typeof(IDeletableEntity).IsAssignableFrom(entityType.ClrType)) .ToList() .ForEach(entityType => { builder.Entity(entityType.ClrType).Property<Boolean>("IsDeleted"); builder.Entity(entityType.ClrType).HasQueryFilter(e => EF.Property<Boolean>(e, "IsDeleted") ==