ef-code-first

Entity Framework One-Many TPH Mapping

泪湿孤枕 提交于 2019-12-07 11:12:03
问题 I'm using a data structure similar to this where type of animal is determined from a discriminator column in the table: public class Farm { public int Id { get; set; } public virtual ICollection<Pig> Pigs { get; set; } public virtual ICollection<Cow> Cows { get; set; } } public class Animal { public int Id { get; set; } public int FarmId? { get; set; } public virtual Farm Farm { get; set; } public string Name { get; set; } } public class Pig : Animal {} public class Cow : Animal {} Mapping:

which way? Database-first , model-first , Code-Only?

风流意气都作罢 提交于 2019-12-07 10:03:08
问题 recently I learned Entity Framework by Pro Entity Framework 4.0 book... Now,I want to write the project by EF ...With these conditionsو which way is better and more flexible? My opinion is the Model-first! however I want to know your opinion? thanks! 回答1: Read this: EF 4.1. Which way to go? It goes through the permutations based on your preferences and situation. My opinion mimics what is stated. If you don't have an existing database and dislike visual designers, code first is for you. If

Map Guid Property to Oracle in Entity Framework Code First

ぐ巨炮叔叔 提交于 2019-12-07 08:38:53
问题 I am trying to map a Guid property to Oracle. Here's its declaration: [Key] [Column(Order = 0, TypeName = "RAW")] [MaxLength(16)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public Guid Id { get; set; } However, I keep getting the following exception: Schema specified is not valid. Errors: (7,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Guid[Nullable=False,DefaultValue=]' of member 'Id' in type 'Model.Test' is not compatible with 'OracleEFProvider.raw[Nullable

Is it acceptable to put seed data in the OnModelCreating method in EF code-first?

社会主义新天地 提交于 2019-12-07 07:57:12
问题 I know how to do seed data with the migrations API in EF 4.3. I've been playing with that all night. However, my ultimate goal is to get my project to the point where a user can pull it from source control and press F5 and they are good to go, database, seed data, and all. Currently code-first does an excellent job of building the DB on a fresh build but seed data isn't inserted until I do Update-Database in the package manager console. At that point it runs the seed method and inserts my

How to create CreatedOn and UpdatedOn using EF Core 2.1 and Pomelo

蹲街弑〆低调 提交于 2019-12-07 07:28:41
问题 In Code First approach, how to define my entity so that: CreatedOn NOT NULL - value is generated on insert by the db with the current timestamp Updated NULL - value is generated on update by the db with the current timestamp Sample Entity: public class MyEntity { public int Id { get; set; } public string Name { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(TypeName = "TIMESTAMP")] public DateTime CreatedOn { get; set; } [Column(TypeName = "TIMESTAMP")] public

Code first: Where's the connection string & the Database?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 07:03:21
问题 I'm testing how code first works. Here's how I defined the Context public class EfDbContext: Context { public DbSet<Client> Clients { get; set; } } I didn't create any data base, but I was able to do all the CRUD operations. Now I don't see the connection string in my web.config. I don't see either the Database. I've checked the App_Data Directory and also the Sql Server Express. I don't see any trace of the Database created. Yet, everything is perfectly. 回答1: EF code-first will use a

Consolidate many if conditions to a single LINQ statement if possible

旧城冷巷雨未停 提交于 2019-12-07 07:03:11
问题 I am currently using ASP.NET MVC and Entity Framework Code First using the repository pattern to display a simple list of users that has paging. I am adding the ability to filter that list by allowing the end user to select conditions from two drop downs and two text boxes with start and end date. Is there a better way to do this than the code below where I have to test for all possible combinations of parameters in order to write the appropriate LINQ query: public IEnumerable<User>

Derive Entity class from a NotMapped class in Entity Framework 4.1 Code First

喜欢而已 提交于 2019-12-07 06:14:02
问题 I need to derive two of my Entity classes from a base class that does not belong to the model. [NotMapped] public abstract class BaseClass { [NotMapped] public string SomeProperty { get; set; } } public partial class Derived1: BaseClass {} public partial class Derived2: BaseClass {} I have tried marking the base class and all its properties as [NotMapped] but the context initializer throw an error saying that both my derived entity classes are not mapped. 回答1: Figured it out. The [NotMapped]

Execute StoredProcedure in CodeFirst 4.1

不羁岁月 提交于 2019-12-07 05:55:44
问题 I understand stored procedures mapping is not supported by my understanding is that I should be able to call stored procedures. I have quite a few complex stored procedures and with the designer I could create a complex type and I was all good. Now in code first let's suppose I have the following stored procedure, just put together something silly to give an idea. I want to return a student with 1 address. In code I have A Student and Address Entity. But no StudentAddressEntity as it's a link

Entity Framework Database Initialization: Timeout when initializing new Azure SqlDatabase

与世无争的帅哥 提交于 2019-12-07 05:37:09
问题 I have an ASP.NET MVC application. When a new customer is created via CustomerController I run a new background task (using HostingEnvironment.QueueBackgroundWorkItem ) to create a new Azure SqlDatabase for that customer. I use Entity Framework Code First to create/initialize the new database. Here's the code: // My ConnectionString var con = "..."; // Initialization strategy: create db and execute all Migrations // MyConfiguration is just a DbMigrationsConfiguration with