ef-code-first

How to know project is code-first or database-first?

十年热恋 提交于 2020-08-27 07:38:40
问题 In an existing project, how do I know if it's code-first or database-first? Project has this lines of code: public class TestDBContext : DbContext { public DbSet<Player> Players { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } And project has no .edmx file. If any other details need I will share. EDIT: Player.cs class public class Player { public int PlayerID { get; set; } public string

Database operation expected to affect 1 row(s) but actually affected 0 row(s)

放肆的年华 提交于 2020-08-27 06:06:50
问题 I'm trying to insert records in two tables, but getting the exception. Could you please help me to resolve the issue. First I tried the below code. await _testRepository.InsertAsync(test); await _xyzRepository.InsertAsync(xyz); Then I tried this code, But nothing is working for me. try { var test = new Test(); using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) { int? tenantId = _unitOfWorkManager.Current.GetTenantId(); using (_unitOfWorkManager.Current.SetTenantId

EntityFramework Core production migrations

人走茶凉 提交于 2020-07-10 05:40:34
问题 How do I update the database for production? I've moved the connection string secrets out of the application. Our CI/CD pipeline handles the token replacement within the connection string "template" that remains in the appsettings, and developers won't have production database access. I had expected to use automatic migrations, only to discover today that Microsoft eliminated that for EF Core. Will I have to, every time, temporarily overwrite the connection string that normally specifies my

EntityFramework Core production migrations

痴心易碎 提交于 2020-07-10 05:38:33
问题 How do I update the database for production? I've moved the connection string secrets out of the application. Our CI/CD pipeline handles the token replacement within the connection string "template" that remains in the appsettings, and developers won't have production database access. I had expected to use automatic migrations, only to discover today that Microsoft eliminated that for EF Core. Will I have to, every time, temporarily overwrite the connection string that normally specifies my

Does an equivalent to Database.CompatibleWithModel(bool) exist in EF Core

不羁的心 提交于 2020-07-06 20:21:57
问题 I'm working on a project that uses EFCore 2.1.0-preview1-final code first approach. Like in EF6 (and previous versions) I want to ensure the compatibility of my DbContext (and models) to the database. In EF6 it was enabled by default and it was possible to deactivate it with Database.CompatibleWithModel(false);. As far as I know EF uses the __MigrationHistory table where the model information was stored. EFCore has no such column in __EFMigrationsHistory table that could provide such

One-to-one and one-to-many relationships with same owned entity type

≡放荡痞女 提交于 2020-06-29 04:14:31
问题 After this question I tried working with owned entity types but I'm stuck at the following scenario: public class User { [Key] public Guid UserId { get; set; } public ICollection<Listing> Listings { get; set; } public Image Avatar { get; set; } } public class Listing { [Key] public Guid ListingId { get; set; } public ICollection<Image> Photos { get; set; } } [Owned] public class Image { public long Size { get; set; } public string Height { get; set; } public string Width { get; set; } }

EF Code first NotMapped Attribute

柔情痞子 提交于 2020-06-16 08:01:39
问题 Why is in the following example the [NotMapped] attribute required: public virtual ICollection<Blog> Blogs { get; set; } [NotMapped] public List<Blog> NewBlogs{ get{ return Blogs.Where(x=>x.Date > DateTime.Now).ToList(); } } Without the [NotMapped] attribute I get an exception: Invalid column name Blog_ID The column name in the database is BlogID. EDIT I would expect, that properties without setter are never directly mapped to the database and automatically ignored by code first. 回答1: with

Why this app is trying to connect to SQL Server instead of SQLite?

泄露秘密 提交于 2020-06-01 05:06:25
问题 Environment : VS2019 , .NET 4.8 , EF 6 , SQLite , WPF App (.NET Framework) I am following this Microsoft official tutorial to create a code-first app, called WPF_EF6 using SQLite. With the configuration shown below, I am getting an error about failing to connect to SQL Server. But as you can see in the App.config file below, there is no SQL Server is involved here? Question : what may I be missing here? And how can we resolve the issue? App compiles fine but throws this runtime error. I

Entity Framework Code First - Cast smallint and integer to int32

柔情痞子 提交于 2020-05-14 18:03:19
问题 Im working on a small service tool for a database. My problem is that due the last update, some smallint-colums had to be be changed into integer. public class TEST { public int ID { get; set; } //public Int16 ID { get; set; } public string TEST { get; set; } } I changed the type from Int16 to int. Everything works fine, except that I can't use it with the old Version of the Database anymore. The Exception is something like "System.Int32 expected, found Typ System.Int16". Is there a way to