ef-code-first

One to one mapping in EF Code First and bi-directional navigation properties

≯℡__Kan透↙ 提交于 2021-02-19 08:26:37
问题 I have been tasked with porting an old app to MVC 3, and want to use EF's code first to replicate its existing database schema. The current code base is littered with hard coded SQL commands, and so the model I come up with has to be "compatible" with what the current system expects. I know I could use EF's Database First to do this, but the existing schema is so simple that I see no reason not to use code first, as I want a solid foundation to build on as we migrate away from our old hard

EF Code First Project Doesn't Create AspNetUsers, AspNetRoles etc Tables after changing DB Connection

感情迁移 提交于 2021-02-19 02:47:06
问题 I've finished an EF Code First Project with a SQL Express database in a lab environment. Once the project is done, I've changed the DB Connection String to connect to the new Production SQL Express Database. Now, I have got the Model tables generated automatically but the AspNetUsers, AspNetRoles, AspNetUserRoles etc. are not generated automatically. I am a new learner and I need your help to know how I can get the tables back in the database. If I do update-database from the Package Manager

Seed Many-to-Many Data in Entity Framework

人走茶凉 提交于 2021-02-17 22:25:32
问题 I am using code first and have a many-to-many relationship between Book Titles and Categories. What is the best way to seed the data during development? If I add two books in the same category, the Seed logic adds the category twice to the category table. I can add categories separately to the Category table, but then how do I specify that existing category records in the books collection of keywords. 回答1: I credit this blog entry with showing me the way http://blog.goranobradovic.com/2011/06

How to use JsonObject of Pomelo.EntityFramework

风流意气都作罢 提交于 2021-02-11 14:15:11
问题 I want to store list of string into mysql table as json. I saw there is support for this in pomelo entityframework. I followed this https://libraries.io/github/tuanbs/Pomelo.EntityFrameworkCore.MySql Here is my entity public class Project { public int Id {get;set;} public string Title {get;set;} public JsonObject<List<string>> Tags {get;set;} } But when _context.Database.EnsureDeleted(); is called it gives below error Navigation property 'Tags' on entity type 'Project' is not virtual.

Entity Framework 6 - Attaching a graph of entities queried with AsNoTracking

我的未来我决定 提交于 2021-02-11 13:47:06
问题 I am retrieving a list of entities from a table FattureFornitori , and loading also a collection owned by them (Voci, plural form - Voce, singular form) and a reference from each Voce to a TipoCosto entity: var db = new DbGestPre(); db.Configuration.ProxyCreationEnabled = false; var s = db.FattureFornitori .Include(x => x.Voci) .Include(x => x.Voci.Select(y => y.TipoCosto)) .AsNoTracking().ToList(); Now, multiple Voci within a single FattureFornitori can reference the same TipoCosto . So,

EF Core - error with duplicated key while adding data with unique index

你。 提交于 2021-02-10 18:51:54
问题 I'm trying to add data to the database using EF Core, but I cannot overcome duplicate key error: Cannot insert duplicate key row in object 'dbo.Stocks' with unique index 'IX_Stocks_Name'. The duplicate key value is (stock1). I have entity Stock that has relation one-to-many with Transaction - one Stock can be related with many Transaction s. The problem doesn't occur only if there's no Stock with a given ID in the database - in that case I can add many Transaction s with the same StockID and

EF 6, code first junction table name

我的未来我决定 提交于 2021-02-10 10:56:32
问题 I am experimenting with custom naming convenctions in EF 6. I have 2 tables and one junction table (WebUser, UserRequest, WebUserUserRequest). I have written function that should be able to rename tables: from WebUser to web_user private string GetTableName(Type type) { var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]); return result.ToLower(); } It is applied this way: modelBuilder.Types() .Configure(c => c.ToTable(GetTableName(c.ClrType))); The function is

EF 6, code first junction table name

霸气de小男生 提交于 2021-02-10 10:52:07
问题 I am experimenting with custom naming convenctions in EF 6. I have 2 tables and one junction table (WebUser, UserRequest, WebUserUserRequest). I have written function that should be able to rename tables: from WebUser to web_user private string GetTableName(Type type) { var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]); return result.ToLower(); } It is applied this way: modelBuilder.Types() .Configure(c => c.ToTable(GetTableName(c.ClrType))); The function is

Entity Framework trying to add migration of tables that are already in database

南楼画角 提交于 2021-02-10 09:14:00
问题 When trying to run my ASP.NET MVC application locally, everything works fine. But once I've deployed it Azure, it keeps giving me the error: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. So I connected to the remote database using the Package Manager Console in Visual Studio and ran the Update-Database command. This gave me the same error. When I look at the database inside the SQL Server Management Studio, it looks

Entity Framework trying to add migration of tables that are already in database

纵饮孤独 提交于 2021-02-10 09:09:26
问题 When trying to run my ASP.NET MVC application locally, everything works fine. But once I've deployed it Azure, it keeps giving me the error: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. So I connected to the remote database using the Package Manager Console in Visual Studio and ran the Update-Database command. This gave me the same error. When I look at the database inside the SQL Server Management Studio, it looks