entity-framework

EF Core creates multiple foreign key columns

半腔热情 提交于 2020-01-16 10:40:06
问题 Im using EF Core with .Net Core 3.1 I have simple example of Client-Event relationship: public class BaseEntity { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public DateTime CreatedOn { get; set; } public DateTime? ModifiedOn { get; set; } } public class Client : BaseEntity { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Phone { get; set; } } public class Event :

sequence of updates after calling context.SaveChanges()

早过忘川 提交于 2020-01-16 09:48:10
问题 Below is an example from my textbook, public class ContactDetails { public long Id { get; set; } public string Name { get; set; } public long SupplierId { get; set; } public Supplier Supplier { get; set; } } public class Supplier { public long Id { get; set; } public string Name { get; set; } public ContactDetails Contact { get; set; } } so the author try to swap supplier between two ContactDetails objects details (let's say its alias is A) and targetDetails (let's say its alias is B) as: /

Edit Method in MVC4 and Visual Studio 2012

点点圈 提交于 2020-01-16 08:52:08
问题 Could someone please help me with the question i posted late last night. I am still stuck with the edit method. Link below: Last night question In addition to what was posted, i have also tried: HttpPost Edit: [HttpPost] public ActionResult Edit(MensPlayer mensPlayer) { if (ModelState.IsValid) { //Save Player _dataSource.Entry(mensPlayer).State = EntityState.Modified; _dataSource.Save(); TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name); return RedirectToAction(

fetch the entire nested model in a single call EF6 MySQL

余生长醉 提交于 2020-01-16 08:21:07
问题 How we can fetch the entire model in a single fetch. Say model is Model.MInner1.MInner2.MInner3 All the inner objects are List. I have only DbSet of Models and need to fetch entire model by loading all inner collection objects. I am getting this correctly with the statement below, also Object1 is loaded correctly. List<Model> models = dbContext.Models.Include("MInner1.Object1") .Include("MInner1.MInner2.MInner3").ToList(); MInner3 collection contains 2 object that need to be loaded. Like

Entity Framework Migration: Why does it ignore snapshot and take __MigrationHistory into account?

风流意气都作罢 提交于 2020-01-16 07:07:10
问题 I'm using EF 6.0.0 and .Net 4.5. I face a very confusing problem. Me and one of my colleagues are working on the domain model section of our project on two different clients. The problem is: 1- Me and my colleagues start with the absolutely identical project and we are completely synced with the source control. 2- When I change the model for example add a property then Add-Migration FromA then Update-Database it works great. The generated code file contains just one command that is to add the

Looping through tables in Entity Framework 6

徘徊边缘 提交于 2020-01-16 06:28:49
问题 sorry for the simple question. I'm new at this. I have an entity model with several tables and I'd like to get a list of the tables, then I'd like to get the contents of a column from the tables. For example, I have tables of subjects: Biology, Chemistry and Physics, each with a column className (among other columns). I'd like to loop through the tables, get the name then get the contents under that column since I need to ToList() it. I want to do something like this: for each (table in

What is the equivalent of the -IgnoreChanges switch for entity-framework core in CLI?

て烟熏妆下的殇ゞ 提交于 2020-01-16 05:52:05
问题 With Visual Studio one can run the command Add-Migration InitialCreate -IgnoreChanges in the Package Manage Console when creating the first migration of the model of an existing database with Code First workflow. What is the equivalent for CLI? The add command would be like this: dotnet ef migrations add InitialCreate but what about the ignore switch? 回答1: In the absence of any other way, one can empty the Up and Down method blocks of the migration of all code and run database update. public

EF Code First: Duplicate foreign keys (one from name convention, one from navigation property)

佐手、 提交于 2020-01-16 05:31:47
问题 I'm trying to create an SQL database by using EF Code First. Assume I have the following code: public class Account { public int Id; public ICollection<User> Users; } public class User { public int Id; public int AccountId; } public class AccountContext : DbContext { public DbSet<Account> Accounts; public DbSet<User> Users; } (Note the lack of any Fluent API commands or Data Annotations; I want to do this by convention.) When the database is created, I get the following fields in the Users

EF “Model First”: Create entity in edmx and db-table from class?

孤街浪徒 提交于 2020-01-16 05:26:17
问题 This question is part of my following one I already asked roughly 2.5 years ago: Entity Framework (.NET) Round-Trip Modelling with Model First? I wanted to ask if in the meantime, the following is possible (and from which version of EF upwards): We are currently using EF 5.0 with the "Model First" approach (VS 2012). After first problems, we managed to configure the two T4 templates for generating the DbContext-derived class and the POCOs so that if we need to map a new (or existing) database

How to write like statements in Entity Framework when the liked valued contains %

…衆ロ難τιáo~ 提交于 2020-01-16 04:55:08
问题 I know that String.Contains('x') would translate to LIKE '%x%' , and String.StartsWith('x') and String.EndsWith('x') would translate into LIKE '%x' and LIKE 'x%' respectively. However, I'm now in a situation in which I'm gonna build the LIKE operator regular expression clause in the business layer, and send it to the SQL Server through Entity Framework. I mean, now the end users builds the regular expression through a GUI and we need to retrieve the result based on that. For example, user