ef-code-first

Serialize one to many relationships in Json.net

你离开我真会死。 提交于 2019-12-28 11:45:11
问题 I am using the Entity Framework code first for data access and I have a Company class which has a collection of Employees. The Employee class also has a Company property. I would like to be able to serialize a Company and include the list of employees in the serialization. Here is Company: public class Company { public long Id { get; set; } public string Name { get; set; } public DateTime? Established { get; set; } public virtual IList<Employee> Employees { get; set; } public DateTime?

Create Stored Procedures using Entity Framework Code First?

旧城冷巷雨未停 提交于 2019-12-28 11:16:45
问题 I am using Entity Framework Code First in my current project. A Database consists of many tables, many views, and many functions. I am able to create tables using Entity Framework Code First. But I am unable to find a way to create stored procedures using Entity Framework Code First. I know Database-First strategy will fulfill my requirement and everything works well, but I don’t want to use Database-First strategy into my existing project code. Please help me someone, what are the best ways

Create Stored Procedures using Entity Framework Code First?

☆樱花仙子☆ 提交于 2019-12-28 11:16:40
问题 I am using Entity Framework Code First in my current project. A Database consists of many tables, many views, and many functions. I am able to create tables using Entity Framework Code First. But I am unable to find a way to create stored procedures using Entity Framework Code First. I know Database-First strategy will fulfill my requirement and everything works well, but I don’t want to use Database-First strategy into my existing project code. Please help me someone, what are the best ways

Generate money type fields using code first EF CTP5

浪尽此生 提交于 2019-12-28 06:17:07
问题 In this blog post: EF4 Code First Control Unicode and Decimal Precision, Scale with Attributes, Dane Morgridge used attributes to control the creation of different types on your database. ...And I found this pretty unique BTW!!! How do I generate money type fields in my resulting database using code first API of EF CTP5, if is possible to do it from your model, using conventions or attributes? Sorry about my English is not my main language. Thanks in advance. 回答1: For example, consider this

Navigation Property without Declaring Foreign Key

霸气de小男生 提交于 2019-12-28 01:55:28
问题 All my models contain at least two associations. When modeling this in ef4 I've only been able to do this without a second Foreign Key property through the use of the fluent interface. ForeignKey seems like the right attribute to use, except for the fact that it requires a string parameter. So my question is, can you have a navigational property and declare it as such using an attribute? public class User : IAuditable { // other code public virtual User Creator { get; set; } public virtual

Entity framework code-first null foreign key

人盡茶涼 提交于 2019-12-27 17:10:06
问题 I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public class User{ public int CountryId { get; set; } public Country Country { get; set; } } public class Country{ public List<User> Users {get; set;} public int CountryId {get; set;} } Error: A foreign key value cannot be inserted because a corresponding

EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

爷,独闯天下 提交于 2019-12-27 11:04:54
问题 I'm attempting to do the following: public class class1 { public int Id {get;set;} [ForeignKey("Class2")] public int Class2Id {get;set;} public virtual Class2 Class2 {get;set;} } public class class2 { public int Id { get; set;} [Required] public virtual int Class1Id {get;set;} [Required] [ForeignKey("Class1Id")] public Class1 Class1 {get;set;} } However every time I try to migrate my database I get the following error: Class1_Class2_Target: : Multiplicity is not valid in Role 'Class2_Class1

Is there any way i can just create a table in SQL Server and then update the migration to my project?

瘦欲@ 提交于 2019-12-25 18:38:32
问题 I have imported database tables from MS SQL Server 2012 and migrated them to Visual Studio 2015 using code-first concept shown in this example. Its working but just now i just added a new table in SQL Server and i wanted to update the DBContext in my project with the new table. I tried entering "add-migration new_table_name" in the Package Manager Console but it didn't work. I understand that i have to code a new model manually in the Models folder of my project, then only that migration will

Insert fk at the same time as attributes are added

你。 提交于 2019-12-25 16:51:44
问题 So im looking to improve my code abit. I currently have two models and two tables that are connected fk and pk. I want to insert new attributes into my two tables, but in that same instant, i want to set the fk in my db called USERS . Currently i cant find another way other than this: using (db) { var encryptedPassword = PasswordStorage.CreateHash(model.Users.PASSWORD); var user = db.USERS.Create(); user.EMAIL = model.Users.EMAIL; user.PASSWORD = encryptedPassword; user.NAME = model.Users

Refactor EF6 entity to instead of use multiple properties use a complex type

心已入冬 提交于 2019-12-25 16:42:57
问题 I have a class customer that contains address properties, phone properties and fax properties, but I want to take off the address, phone properties to complex types. Does properties are already in the database as columns. [Table("tblCustomer")] public partial class Customer : Entity { [Key] public int CustomerID { get; set; } [StringLength(10)] public string CustomerCode { get; set; } [StringLength(60)] public string AddressLine1 { get; set; } [StringLength(70)] public string AddressLine2 {