Click here to Skip to main content
15,033,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
how can i create One-to-One Relationship in First Code?

i did this , is it true or Not :
ProjectMap.cs
C#
public class ProjectMap
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ProjectId { get; set; }
        public string Name { get; set; }
        public string Famil { get; set; }
       
        public virtual ProjectDis ProjectDis { get; set; }  
    }


and in ProjectDis.cs
C#
public class ProjectDis
    {
        [key]
        public int Id { get; set; }
        public string PostCode { get; set; }
        public string Entry_Date { get; set; }
       
        [ForeignKey("ProjectMap")]
        public virtual ProjectMap ProjectMap {get;set;}
    }

one-------------> one or zero
ProjectMap -----> ProjectDis
Posted
Updated 12-Jul-14 19:46pm
v2
Comments
NorouziFar 13-Jul-14 22:44pm
   
no body ?????

Hi NorouziFar,

I don't see any value in asking such questions on QA. If you want to use the "Code First" Approach in EF you have to learn how the conventions and attributes work. Better go through some basic tutorial. This was first hit on goolge for me: http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx[^]
   
   
Both tables can have only one record on either side of the relationship. Each primary key value relates to only one record (or no records) in the related table. Keep in mind that this kind of relationship is not very common and most one-to-one relationships are forced by business rules and don't flow naturally from the data. In the absence of such a rule, you can usually combine both tables into one table without breaking any normalization rules.

There is a my article that explains how can use one to one relationship and zero relstionship using Entity Framework code first approach.

Relationship in Entity Framework Using Code First Approach With Fluent API[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900