ef-code-first

How do I examine the member configurations of an EntityTypeConfiguration<T> instance?

我的未来我决定 提交于 2020-01-04 01:08:11
问题 I'm using Entity Framework Code First with map classes inheriting from EntityTypeConfiguration. I do this to encapsulate my use of the Code First fluent API for configuring entities. I would like to be able to examine the configuration settings made in these classes, so that I can apply some of them in my integration testing. I'm using AutoFixture to create entities quickly, and ultimately I want to figure out a way to make some customizations that consume the configurations inside of my

How to change the auto-generated “Discriminator” column in EntityFramework?

牧云@^-^@ 提交于 2020-01-03 18:51:53
问题 I am working on some code that makes use of single table inheritance via EF4.3. There is an entity called User and another entity called Admin. Admin inherits from user. User class public class User { public int Id {get;set;} public string Username {get;set;} } Admin class public class Admin : User { } EF generated the database tables with a Discriminator column. When an Admin record is added then Discriminator="Admin" and a User record would have Discriminator="User" . My question is, how do

How to change the auto-generated “Discriminator” column in EntityFramework?

人盡茶涼 提交于 2020-01-03 18:51:17
问题 I am working on some code that makes use of single table inheritance via EF4.3. There is an entity called User and another entity called Admin. Admin inherits from user. User class public class User { public int Id {get;set;} public string Username {get;set;} } Admin class public class Admin : User { } EF generated the database tables with a Discriminator column. When an Admin record is added then Discriminator="Admin" and a User record would have Discriminator="User" . My question is, how do

How to log SQL using entity framework 4.3 (code first) and SQL Azure database

妖精的绣舞 提交于 2020-01-03 15:34:36
问题 I realize this similar question has been asked a few times, and I have tried the recommendations in those questions without success. I am using the entity framework (4.3) and running against SQL Azure (on a federated database). I'd like to be able to log the SQL that is being generated by the entity framework. I've used the Entity Profiler Framework and while that is helpful during development, I am not sure it would be helpful during production. I can't use SQL Profiler as this is a SQL

How do you set Entity Framework to cascade on delete with optional foreign key?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 12:24:30
问题 I'm attempting to set Entity Framework to cascade on delete with an optional foreign key. I'm using code first, and my model looks like this: public class Node { [Key] public int ID { get; set; } [ForeignKey("Parent")] public int? ParentID { get; set; } public virtual Node Parent { get; set; } } I've seen plenty of solutions that suggest, "Just make the foreign key required," but this will not work for me because the parent node may be null. Does a solution exist that doesn't involve manually

Entity Framework 4.3 Migrations move existing data

◇◆丶佛笑我妖孽 提交于 2020-01-03 10:40:51
问题 I'm using EF Code First 4.3 Migrations to update my database scheme. Now I have the following situation: table A needs to be removed, table B must be created and the data of table A must be copied (along with some other data) to table B. I do not have access to the DbContext in the DbMigration class, my question is how to implement this? 回答1: In migration Up method of your migration you can use Sql method to define any SQL you need so if you use explicit migration you can put data migration

Migrating data when adding one-to-one relationship in EF Core?

社会主义新天地 提交于 2020-01-03 08:49:13
问题 After adding a new one-to-one relation to one of my table I cannot figure out how to add default data for existing rows in my database. My database basically looked like this before upgrade: -- Team -- Name: TEXT -- History -- Id: INT ... where History is has foreign keys pointed to it from other unrelated tables. In my upgrade I basically want a Team to have a single History so my new db looks like: -- Team -- Name: TEXT HistoryId: INT -- History -- Id: INT My problem now, however is that I

EF Code First Custom Collections

*爱你&永不变心* 提交于 2020-01-03 08:45:13
问题 When creating code first collections can you implement a custom class that implements ICollection. The code below is conceptual not actual public class Product { public int ProductId { get; set; } public string Name { get; set; } public Category Category { get; set; } } public class Category { public int CategoryId { get; set; } public string Name { get; set; } //Want to Avoid This public ICollection<Product> Products { get; set; } //Use his instead of above public ProductList

EF6: How to generate a unique number automatically based on entity Id

╄→гoц情女王★ 提交于 2020-01-03 05:25:08
问题 Consider an entity like this: public class Document { public int Id { get; set; } public long code { get; set; } // Rest of Props } What I need is to generate a unique long code, and I prefer to generate it based on Id. One simple classic but unsafe solution is to get the last Id and increase it by one and use, but I'm looking for a better solution, such as computed column, or any other way to assign code generation to database. I cannot access to Id if I try define it as a compute column.

Is it possible perform actions in POCO before insert/update?

六月ゝ 毕业季﹏ 提交于 2020-01-03 00:25:52
问题 Let's assume this Entity Framework sample: public class User { public int UserID { get; set; } public string Name { get; set; } } public class MyDbContext : DbContext { public DbSet<User> Users { get; set; } } class Program { static void Main(string[] args) { using (var db = new MyDbContext ()) { var user = new User { Name = "Foo"}; db.Users.Add(user); db.SaveChanges(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } } I want add to User class events, like "BeforeInsert"