code-first

How to determine the order of Property Loading in CodeFirst?

倾然丶 夕夏残阳落幕 提交于 2019-12-25 07:23:42
问题 I was thinking that is there any way to determine the order of property loading in CodeFirst,for example i have a class like below: public Class { public string Propert1{get;set;} public string Propert2{get;set;} public List<string> PropertList{get;set;} } And i need to make EF to load ProprtyList before property1!(Because i manipulate ProprtyList Values On Property1_Changed). 回答1: A property should be just that: a property. I.e. you get or set it and nothing else, no side effects . I know

creating 1-1 relationship between two class using code first and migration

∥☆過路亽.° 提交于 2019-12-25 07:00:13
问题 Well, it is 1st time i am trying to create 1-1 relationship between two tables using code first. I took some help online and come across the following classes mapping. Than I ran migration and found something wrong. E.g. The migration says that primary key for StudentDetails is Id from Student table whereas I am looking to have primary key StudentId. Also, the foreign key is being created in opposite way. Please can someone highlight what is wrong here or is it me who perceived it wrong. I

Data is modified without my consent

被刻印的时光 ゝ 提交于 2019-12-25 06:55:53
问题 I am using .NET MVC 5 and entity framework I have this model : public class Division { public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } public virtual IList<Department> Departments { get; set; } [Display(Name = "Manager")] public string ManagerId { get; set; } [ForeignKey("ManagerId")] public virtual AppUser Manager { get; set; } public virtual IList<AppUser> Users { get; set; } } So the Division Has a Manager. When I keep the App running for a while,

How to change string key to int

十年热恋 提交于 2019-12-25 04:22:43
问题 I use C# EntityFramework and CodeFirst. Also I use migrations (automatic & manual migrations). I have a model - public class MyClass { public string Key { set; get; } public string Something { set; get; } } And I already have a database with rows. Now I want to change the model : a. Change "Key" column to "Name" (which is not a problem with migration, especially automatic migrations) b. Add a new property - an int "Key" that will be identity (1, 2, 3...). How can I do that without deleting

The data reader is incompatible with the specified class

a 夏天 提交于 2019-12-25 03:54:35
问题 so there are a few other similar type posts about, but none matching the simple task I am attempting. just running a stored proc through EF6 code first in the following manner var results = context.DataBase.SqlQuery<MyClass>("exec spGetStuff @param1, @param2", new SqlParameter[] {new SqlParameter("param1",value), new SqlParameter("param2", value2)}); I have used this method on many occasions with no issue. the class I am mapping the results to is pretty nasty with many properties, but all

How do I add a reference to another existing entity using code-first framework without first pulling the existing entity from the database

我们两清 提交于 2019-12-25 03:46:05
问题 I'm trying to attach an existing product to an auction, but am unable to do so without first pulling the product from the database. This code works, but how would I go about just providing the productid and then saving the auction var product = new Product() { //Known existing product id ProductId = 1 }; var auction = new Auction { BuyItNowPrice = 10. Product = product, ... ... }; using (var db = new DataContext()) { var product = db.Products.Find(auction.Product.ProductId); auction.Product =

Entity Framework Code First - Default values on non-nullable types

一世执手 提交于 2019-12-25 03:37:15
问题 i'm trying to map a legacy database with a legacy POCO model. Since database and model where develop not having in mind Entity Framework there are some little differences between that made me stuck. The challenge I'm facing is that I would like to make it work being as less invasive as possible (don't want to touch code of the database nor the model) since there is too much codes that depends on. I have tried to map the entities using a code first approach, reusing the POCOs from the legacy

EF Codefirst get POCO's configuration

主宰稳场 提交于 2019-12-25 03:34:10
问题 I have a POCO object (EF Code-first) public class ExampleTestOfDataTypes { public string StringProp { get; set; } } Than i have its configuration class: public class ExampleTestOfDataTypesConfig : EntityTypeConfiguration<ExampleTestOfDataTypes> { public ExampleTestOfDataTypesConfig() { ToTable("CustomTableName"); this.Property(m => m.StringProp).HasColumnName("CustomString"); } } How can i get the new Tablename and new StringProp name when i changed it in config file (considering i have

Entity and mapping using Entity Framework 4 how to handle null (ICollection)?

纵饮孤独 提交于 2019-12-25 03:21:04
问题 I have this fellowing entity : public class Post { public long PostId { get; private set; } public DateTime date { get; set; } [Required] public string Subject { get; set; } public User User { get; set; } public Category Category { get; set; } [Required] public string Body { get; set; } public virtual ICollection<Tag> Tags { get; private set; } public Post() { Category = new Category(); } public void AttachTag(string name, User user) { if (Tags.Count(x => x.Name == name) == 0) Tags.Add(new

Getting Security Exception SQLCE 4, Code First CTP 5 & Shared Hosting

故事扮演 提交于 2019-12-25 03:14:14
问题 I've been trying to setup a webpage with MVC3 Razor, SQLCE 4 and Code First, the last two downloaded from Nuget, so it is EF Code First CTP5. I've had a lot of trouble, but I manage to move forward by following this answer However now I am getting this Security Exception Description : The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust