entity-framework-core

Entity Framework Core Code-First: Cascade delete on a many-to-many relationship

时间秒杀一切 提交于 2019-12-04 03:10:21
I'm working on an ASP.NET MVC 6 project with Entity-Framework Core (version "EntityFramework.Core": "7.0.0-rc1-final" ) backed by a SQL Server 2012 express DB. I need to model a many-to-many relationship between a Person entity and an Address entity. As per this guide I modeled it with a PersonAddress join-table entity, because this way I can store some extra info. My goal is to set-up my system this way: If a Person instance is deleted, all the related PersonAddress instances must be deleted. All the Address instances they reference to must be deleted too, only if they are not related to

Run migrations on azure for dotnet core site deployed with GitHub

夙愿已清 提交于 2019-12-04 03:07:14
I have a small website built with .Net core that includes a SQLite database and entity framework core. I'm using VS Code and on a mac. It is easy to manage the database locally - dotnet ef database update works great. The problem is running migrations when deploying to Azure. My repo is on GitHub, and I configured Azure to pull code from GitHub when I push to the master branch. The deploy is working fine, but migrations aren't running on Azure. I've seen some suggestions that I can use yourDbContext.Database.Migrate() in Startup.cs, however it appears that .Migrate() is no longer available. I

Avoid Entity Framework Error with Multiple Tasks Running Concurrently on Same DbContext

我怕爱的太早我们不能终老 提交于 2019-12-04 02:22:46
I have a WebApi controller in a Dotnet Core project running Entity Framework Core with Sqlite. This code in an action occationally produces errors: var t1 = _dbContext.Awesome.FirstOrDefaultAsync(a => [...]); var t2 = _dbContext.Bazinga.FirstOrDefaultAsync(b => [...]); var r1 = await t1; var r2 = await t2; The errors have been: Microsoft.EntityFrameworkCore.Query.RelationalQueryCompilationContextFactory:Error: An exception occurred in the database while iterating the results of a query. System.ObjectDisposedException: Safe handle has been closed Microsoft.EntityFrameworkCore.Query

one-to-many relation using two columns in Entity Framework Core

∥☆過路亽.° 提交于 2019-12-04 01:56:35
In my project I have a table Translation that can have translations for any model. To achieve this, the table has two fields: Model and ModelId . The Model property holds an integer indicating the type of the model and the ModelId has the id of this model. So, for example: the Product table has modeltype id 1 . To get all translations for a product with id 317 , I search for translations with Model=1 AND ModelId=317 . Now I would like to create this relation in Entity Framework Core. All my models inherit from the class BaseModel that has a property ModelType holding the id of the model type.

What is a proper way of writing entity POCO classes in Entity Framework Core?

孤街醉人 提交于 2019-12-04 01:55:25
EF Core has a "code first mentality" by default, i.e. it is supposed to be used in a code-first manner, and even though database-first approach is supported, it is described as nothing more than reverse-engineering the existing database and creating code-first representation of it. What I mean is, the model (POCO classes) created in code "by hand" (code-first), and generated from the database (by Scaffold-DbContext command), should be identical. Surprisingly, official EF Core docs demonstrate significant differences. Here is an example of creating the model in code: https://ef.readthedocs.io

EFCore nullable relationship setting onDelete: ReferentialAction.Restrict

廉价感情. 提交于 2019-12-04 00:26:48
问题 I'm running efcore 2.0.1. I have a model: public class BigAwesomeDinosaurWithTeeth { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public ICollection<YummyPunyPrey> YummyPunyPrey { get; set; } } public class YummyPunyPrey { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public Guid? BigAwesomeDinosaurWithTeethId { get; set; } [ForeignKey("BigAwesomeDinosaurWithTeethId")] public BigAwesomeDinosaurWithTeeth

Scaffold-DbContext creating model for table without a primary key

社会主义新天地 提交于 2019-12-04 00:16:36
I am trying to create DBcontext and corresponding model for a particular table in ASP.NET core MVC application. This table doesn't have any primary key. I am running following Scaffold-DbContext command- Scaffold-DbContext "Server=XXXXX;Database=XXXXXXX;User Id=XXXXXXX;password=XXXXXXX" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t TABLE_NAME -force -verbose In Package Manager Console, I can see this verbose output- ............... ............... Unable to identify the primary key for table 'dbo.TABLE_NAME'. Unable to generate entity type for table 'dbo.TABLE_NAME'. My

EF Core 2.0 Identity - Adding navigation properties

北城以北 提交于 2019-12-03 23:11:41
问题 In EF Core 2.0 Identity navigation properties are not included by default, so after upgrading, I added them. So for Many-To-Many relationship between User and Role, and One-To-Many relationship between Role and RoleClaim, I added following navigation properties: public class User : IdentityUser { [Required] public string Name { get; set; } public virtual ICollection<IdentityUserRole<string>> Roles { get; set; } } public class Role : IdentityRole { [Required] public string Name { get; set; }

EF7 change connectionstring at runtime

此生再无相见时 提交于 2019-12-03 23:09:52
问题 In the previous versions of EF we were able to alter the dbcontext connection string as below : context.Database.Connection.ConnectionString = "the new connectionstring"; How can we do this with EF7? Thank you 回答1: I found the solution : https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code Context Code public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } public DbSet<Blog> Blogs { get; set;

EntityTypeBuilder does not contain a definition for ToTable in EF Core

五迷三道 提交于 2019-12-03 22:02:40
I have this sample code: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Models; namespace MySampleNamespace { public class MyDbContext : DbContext { public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } public DbSet<User> Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { new UserMap(modelBuilder.Entity<User>()); } public class UserMap { public UserMap(EntityTypeBuilder<User> entityBuilder) { entityBuilder.ToTable("User"); entityBuilder.Property(s => s.Username).HasMaxLength(15)