Skip to content

Instantly share code, notes, and snippets.

@shunnien
Last active December 4, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shunnien/f63486a52e63225660a60dad6f12ba4d to your computer and use it in GitHub Desktop.
Save shunnien/f63486a52e63225660a60dad6f12ba4d to your computer and use it in GitHub Desktop.
Entity Framework v6 Composite keys Annotation
void Main()
{
string connstr = @"data source=(LocalDb)\MSSQLLocalDB;initial catalog=Northwind;Integrated Security=True;application name=EntityFramework";
var entity = new NorthwindEntities(connstr);
var regionInstance = entity.Regions.Take(2).Dump();
var OrderDetailsInstance = entity.OrderDetails.Take(2).Dump();
}
public class NorthwindEntities : DbContext
{
public DbSet<Regions> Regions { get; set; }
public DbSet<OrderDetails> OrderDetails { get; set; }
public NorthwindEntities()
: base("name=NorthwindEntities")
{
}
public NorthwindEntities(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
}
// Define other methods and classes here
[Table("Region")]
public class Regions
{
[Key]
public int RegionID { get; set; }
public string RegionDescription { get; set; }
}
[Table("Order Details")]
public class OrderDetails
{
[Key]
[Column(Order = 1)]
public int OrderID { get; set; }
[Key]
[Column(Order = 2)]
public int ProductID { get; set; }
public decimal UnitPrice { get; set; }
public short Quantity { get; set; }
public float Discount { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment