entity-framework-core

Rename a foreign key in Entity Framework Core without dropping data

落爺英雄遲暮 提交于 2019-12-03 07:56:27
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_User_Surveys_SurveyId". The conflict occurred in database "AppName", table "dbo.Survey", column

Entity Framework Core - Customise Scaffolding

痴心易碎 提交于 2019-12-03 07:22:00
In Entity Framework 6 we can add the T4 templates the scaffolding uses by running Install-Package EntityFramework.CodeTemplates.CSharp But in Entity Framework Core the scaffolding system does not appear to use T4 templates, nor does it seem like the scaffolding can be customised. It seems to be all in c# classes eg. https://github.com/aspnet/EntityFramework/blob/a508f37cf5a0246e9b92d05429153c3d817ad5ec/src/Microsoft.EntityFrameworkCore.Tools.Core/Scaffolding/Internal/EntityTypeWriter.cs Is there any way to customise the output from the scaffold? bricelam There is a special, yet-to-be

How do I configure an Entity Framework interceptor in an aspnet vnext config.json file?

点点圈 提交于 2019-12-03 07:02:45
In a web config file, I would do this: <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> <interceptors> <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"> <parameters> <parameter value="C:\tmp\DataAccessLogOutput.txt" /> </parameters> </interceptor> </interceptors> </entityFramework> But how do I do it in vnext's

One-to-One relationships in Entity Framework 7 Code First

纵然是瞬间 提交于 2019-12-03 06:51:53
How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog) .HasForeignKey<BlogImage>(b => b.BlogForeignKey); } } public class Blog { public int BlogId { get; set; } public string

Create Tables on runtime in EF Core

最后都变了- 提交于 2019-12-03 06:43:01
I know I can use the following command to add a new migration and create the database : dotnet ef migrations add MigrationName -c DbContextName dotnet ef database update -c DbContextName but I need to create my database/tables on runtime. First I tried to do that by overriding OnModelCreating but I failed. It returned me an error pointing out that the tables have not been created Here is my dbcontext class public class AimeLoggerContext : DbContext { public AimeLoggerContext(DbContextOptions<AimeLoggerContext> options) : base(options) { Database.Migrate(); } protected override void

How to Add an implementation of 'IDesignTimeDbContextFactory<DataContext>' to the project in asp.net-core-2.0

南楼画角 提交于 2019-12-03 05:41:45
Here are the list of packages which I have installed : Installed Packages I am using Entityframework core 2.0. First time I have successfully created database using entity framework code first migration(add-migration and update-database command). Now when I update my entities and try to run migration it's giving me following error. Unable to create an object of type 'DataContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. Here is my code ... Program.cs public

EF Core - Table '*.__EFMigrationsHistory' doesn't exist

半腔热情 提交于 2019-12-03 05:30:58
I want to stress out that this is .NET Core and the threads about EF 6.0 does not apply to this problem I created my DbContext and added it in DI, however when I do dotnet ef database update -v it does not want to create the migrations table __EFMigrationsHistory . Is there some other command that I should do first or this is a bug of EF Core MySQL adapter? MainDbContext using Microsoft.EntityFrameworkCore; using MySQL.Data.EntityFrameworkCore.Extensions; using Web.Models; namespace Web.Infrastructure { public class MainDbContext : DbContext { public DbSet<User> Users { get; set; } protected

Azure Function, EF Core, Can't load ComponentModel.Annotations 4.2.0.0

最后都变了- 提交于 2019-12-03 05:26:45
I have created several .Net Standard 2.0 libraries, tested the execution via a console application, as well as several tests - all is good. Move over to azure function, and get the following run-time error: I then try to download that specific version into the API Function project: I'm using Visual Studio Version 15.7.0 Preview 5.0. I have updated the Azure Function to 4.7... as the console and test projects are - and those work. Been at this a far too many hours.. so I'm hoping the resolution isn't something crazy. Ef Core 2.1.0-rc1-final is also in the mix. Using data annotations for

Where is the dotnet command executable located on Windows?

萝らか妹 提交于 2019-12-03 05:05:11
I am exploring the new Entity Framework Core (NOT in conjunction with ASP.Net, what I am coding is just a WinForms app) and found some tutorials mentioning a dotnet command line command needed to create "migrations". When I try it, however, it says 'dotnet' is not recognized as an internal or external command, operable program or batch file. I have searched my hard drive for "dotnet.exe", "dotnet.bat" and "dotnet.cmd" but have found nothing. I use Visual Studio 2015 Community Edition. Where do I find this command executable? What am I to add to the %PATH% environment variable for it to work?

Proper way to get current User ID in Entity Framework Core

点点圈 提交于 2019-12-03 04:57:33
There are a bunch of different answers floating around here for the different RC's of ASP.NET Core on how to get the ID of the currently logged in user. I wanted to ask the definite question here. Please note that project.json now has "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0" With RC1, you could do something like this: using Microsoft.AspNet.Identity; using System.Security.Claims; User.GetUserId(); But with the newly released version 1 of EF Core, Microsoft.AspNet.Identity is not the right version. There was suggestions to use UserManager, which seems like a lot just to get