This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StudentAddress | |
{ | |
public int Id { get; set; } | |
public string Address { get; set;} | |
public int StudentId { get; set; } | |
public virtual Student Student { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void CreateStudent() | |
{ | |
Student s = new Student | |
{ | |
Id = 1, | |
Age = 12, | |
Name = "Foo" | |
}; | |
StudentAddress sa1 = new StudentAddress { Address = "Delhi", Id = 1 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Context : DbContext | |
{ | |
public Context() | |
: base("name=democonnectionstring") | |
{ | |
} | |
public DbSet<Student> Students { get; set; } | |
public DbSet<StudentAddress> StudentAddresses { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
CreateStudent(); | |
Console.WriteLine("Student Created"); | |
Context c = new Context(); | |
var result = from r in c.Students.Include("StudentAddresses") select r; | |
foreach (var r in result) | |
{ | |
Console.WriteLine(r.Name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Context() | |
: base("mydb") | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<connectionStrings> | |
<add name="democonnectionstring" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Demo1;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/> | |
</connectionStrings> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Context() | |
: base("name=democonnectionstring") | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Student | |
{ | |
public Student() | |
{ | |
Subjects = new HashSet<Subject>(); | |
} | |
public int Id { get; set; } | |
public string Name { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Context : DbContext | |
{ | |
public Context() | |
: base("name=democonnectionstring") | |
{ | |
} | |
public DbSet<Student> Students { get; set; } | |
public DbSet<Subject> Subjects { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void CreateStudent() | |
{ | |
Student s = new Student | |
{ | |
Id = 1, | |
Age = 12, | |
Name = "Foo" | |
}; | |
Subject s1 = new Subject { Id = 1, Name = "Phy" }; |