code-first

Entity Framework Code First internal class - is it possible?

北战南征 提交于 2019-12-10 17:33:57
问题 Is it possible to have Code First data classes declared with internal access as shown: internal class Person { public int Id { get; set; } public string Name { get; set; } } I have a requirement that classes and its properties should not be visible outside of the assembly. 回答1: As long as your DbContext derived class that exposes your class to EF is in the same assembly, you should be able to. I don't happen to design my apps that way as I prefer more separation. But the context should be

How to Get Full Object with Code First Entity Framework 4.1

心已入冬 提交于 2019-12-10 16:55:43
问题 I am trying to return as JSON the fully deep object (with all of the foreign key relationships filled in) but I am getting nulls for all the referenced objects. Here is the call to get the object: public ActionResult GetAll() { return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet); } And here is the Order object itself: public class Order { public int Id { get; set; } public Patient Patient { get; set; } public CertificationPeriod CertificationPeriod { get; set; } public Agency Agency

Entity Framework Code First One-To-Many Relation Mapping with link table

徘徊边缘 提交于 2019-12-10 16:50:05
问题 I have a question about One-To-Many relationship between two tables when there is the link (join) table between. Example tables: ChildTable: ID int NOT NULL PK Relation int NOT NULL ParentTable: ID int NOT NULL PK Name nvarchar(50) NOT NULL ParentChildren: ParentTable_ID int NOT NULL PFK ChildTable_ID int NOT NULL PFK Entities: public class ChildTable { public ChildTable() { this.ParentTables = new List<ParentTable>(); } public int ID { get; set; } public int Relation { get; set; } public

Does the Entity Framework code first support readonly navigation property

谁都会走 提交于 2019-12-10 15:44:23
问题 Currently I use Entity Framework code first to create my domain models. As the code below illustrates, I want create a one-to-many association between "Test2" class and "Test1" class. But when I ran the application, it threw an exception: The navigation property 'T2' is not a declared property on type 'Test1'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property. If I modify the navigation property "T2" to make it have a "protected" or

The cast to value type 'DateTime' failed because the materialized value is null

泪湿孤枕 提交于 2019-12-10 15:39:11
问题 My project is the throwing the above error because the Ship Date in my table is null. I am using Code First, and the DateTime field is declared as nullable both in the generic set and the SQL table. I am populating a gridview with the result set. Ideally, I would like to have the gridview display "not yet shipped" when the ShipDate field is null, but at this point, I would be happy just to be able to display the record. Any help is greatly appreciated. Here is the code-behind and the context

Code First issue with seeding data in Cyrillic

浪子不回头ぞ 提交于 2019-12-10 13:57:49
问题 I am using the seed method to populate the database. Some of the objects have properties in cyrillic. Example: context.Wines.AddOrUpdate(new Wine() { Id = 1, Name = "Шардоне", etc........................................ The objects are properly created in the database, however when I check the database in MS SQL management studio instead of the wine's name being "Шардоне", it is displayed as "Øàðäîíå". Strangely, when I create the wine in my Startup class like this: var db = new

entity framework code first attributes in combination with fluent api configurations

☆樱花仙子☆ 提交于 2019-12-10 13:46:46
问题 Can I use code first attributes in combination with fluent-API configurations for my entities in Entity Framework? Thank you. 回答1: Yes you can. I generally prefer to define some constraints (for example, making a property required by using [Required] or to define a length for a string property by using StringhLength(1, 10) ): [Required] [StringLentgh(1,10)] public string BookName {get;set;} On the other hand, I generally use fluent api to define the relationships (for example, 1-to-many

EF4 code-first vs model-first with regards to model validation

给你一囗甜甜゛ 提交于 2019-12-10 13:38:01
问题 I'm working on a one-man ASP.NET MVC 3 project (I have complete control over database schema and code), and I'm trying to decide between going database-first and POCO w/ my EF4 models, or if I should go w/ code-first. The main thing I'm trying to achieve is decorating my models with DataAnnotation attributes so that I can enforce schema validation prior to doing any persistence. Looking at Scott Guthrie's article about model validation w/ MVC 2, he talks about article about doing it with code

Multiple Inheritance (maybe Abstract class?) C# Entity Framework

微笑、不失礼 提交于 2019-12-10 12:14:01
问题 The last weeks I have been working on the development of a database based on Entity Framework 6 (Code-First) on c# using Visual Studio 2015. I'm currently working on all the options that inheritance offers to work with. At this point and following the standards of a database I should implement multiple inheritance with Primary Key. What this means? This means that I should implement a class that inherits from another class and have his own PK for identify him different from the parent class.

EF code first List<DateTime> not creating table

核能气质少年 提交于 2019-12-10 11:44:35
问题 I am creating a very simple EF4 code first project with one entity. public class Activity { public Guid Id { get; set; } public string Description { get; set; } public List<DateTime> DateDone { get; set; } <snip> public Activity() { if(DateDone==null) DateDone = new List<DateTime>(); } } I rebuild my project and then run the MVC3 app and the database generates (confirmed, I have added and removed columns), my data is seeded (it changes on screen when I modify the seeded data) I am able to: