code-first

SQL 'time' type in Entity Framework Code First

試著忘記壹切 提交于 2019-11-28 20:24:04
I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity: public class ShiftDetail { public long Id { get; set; } [Required] public int DayOfWeek { get; set; } [Required] [Column(TypeName="time")] public DateTime StartTime { get; set; } [Required] [Column(TypeName = "time")] public DateTime EndTime { get; set; } public long ShiftId { get; set; } public virtual Shift Shift { get; set; } } As you can see I'm trying to set the database type for the columns StartTime and EndTime to "time" but I get this error: (112,12) : error 2019: Member Mapping

Generate SQL CE database from EF Code-First DbContext class

↘锁芯ラ 提交于 2019-11-28 19:19:54
问题 I've defined a set of classes in the style of Entity Framework Code-First conventions and annotated the class properties with System.ComponentModel.DataAnnotations attributes. Now I want to generate a SQL Server Compact Edition (SCSE) (4.0) database from this code. What code is needed to do this and what assemblies need to be imported? So far I've imported the C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop{System.Data.SqlServerCe.dll,System.Data.SqlServerCe.Entity

How to ignore a property when using Entity Framework Code First [duplicate]

浪尽此生 提交于 2019-11-28 19:04:39
This question already has an answer here: Ignoring a class property in Entity Framework 4.1 Code First 2 answers Entity Framework Code First will auto-create a table in the database base based on the Model. Is there an attribute that will avoid this? SLaks Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property. drzaus Per the accepted answer and similar question/answer , in addition to [NotMapped] you can also specify it using the Fluent API: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<TheModelAffected>().Ignore

EF Code First Parent-Child insertions with identity columns

让人想犯罪 __ 提交于 2019-11-28 17:53:18
i have the following model. class Parent { int ParentId (identity column) { get; set; } string ParentName { get; set; } virtual ICollection<Child> Children { get; set; } } class Child { int ChildId (identity column) { get; set; } string ChildName { get; set; } int ParentID { get ; set; } //foreign key to Parent(ParentID) } How do i insert few rows to my parent and child in single transaction? Basically i want to get the identity generated on the parent(say i insert a row in parent) and insert child rows with that value? How this can be achieved using Code First? You shouldn't worry about what

using Guid as PK with EF4 Code First

末鹿安然 提交于 2019-11-28 17:27:04
I have this class and table: public class Foo { public Guid Id {get;set;} public string Name {get;set;} } create table Foo ( id uniqueidentifier primary KEY DEFAULT (newsequentialid()), name nvarchar(255) ) the problem is that when i try to save new foo the first one goes with the 0000-000-00 ... id and the second also, so I get constraint exception anybody knows a fix ? Devart Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); or using the

Adding CreatedDate to an entity using Entity Framework 5 Code First

放肆的年华 提交于 2019-11-28 17:14:50
I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UTC date. I do NOT want to use a constructor, as I have many entities in my model that I want to inherit from an abstract class containing the CreatedDate property, and I can't enforce a constructor with an interface. I have tried different data annotations and I have attempted to write a database initializer that would pick up a specific entity type and write an alter constraint with a getdate() default value for the correct table_name

Entity Framework Code First & Search Criteria

眉间皱痕 提交于 2019-11-28 17:07:11
So I have a model created in Entity Framework 4 using the CTP4 code first features. This is all working well together. I am attempting to add an advanced search feature to my application. This "advanced search" feature simply allows the users to enter multiple criteria to search by. For example: Advanced Product Search Name Start Date End Date This would allow the user to search by the product name and also limit the results by the dates that they were created. The problem is that I do not know how many of these fields will be used in any single search. How then can my Entity Framework query

How can I get my database to seed using Entity Framework CodeFirst?

巧了我就是萌 提交于 2019-11-28 15:20:49
The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions? On a side note, is it possible to call the initializer without having a reference to my DatabaseContext in the client? I have included all the relevant code I could think of. If anything else would be helpful, please let me know. Things I've Tried: I deleted my connection string (since it defaults to sqlexpress anyways, just the name changed) I changed DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways,

Entity Framework Code First Case Sensitivity on string PK/FK Relationships

三世轮回 提交于 2019-11-28 12:08:29
I have a fairly simple composite one to many relationship defined using POCO/Fluent API, one column of which is a string. I've discovered that the data in this column in our database is inconsistent in terms of case ie 'abb', 'ABB' - this is our main ERP system and is fed by a variety of sources which are mainly beyond our control. This is leading to problems using EF code first when joining to related tables as the join is silently ignored by EF when the case of PK/FK is different even though SQL Profiler shows the correct SQL being executed and results returned. I'm using WCF so have lazy

Entity Framework mapping

跟風遠走 提交于 2019-11-28 11:37:25
问题 I've made simple classes that simulate the classes I have (sorry I had to make up the classes, the usual example databases do not have the structure I wanted to ask about): public class Animal { public System.Guid ID { get; set; } public string SpeciesName { get; set; } public virtual ICollection<AnimalSpecies> AnimalSpecies { get; set; } } Species Fish: public class Fish { public System.Guid ID { get; set; } public int Freshwater { get; set; } } Spieces Reptile: public class Reptile { public