Created
July 25, 2011 20:53
-
-
Save mverzilli/1105181 to your computer and use it in GitHub Desktop.
Context aware entities in Entity Framework
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 partial class CountryShardedDatabase | |
{ | |
public static CountryShardedDatabase FromCountry(string country) | |
{ | |
var db = new CountryShardedDatabase(string.Format("name={0}", ConfigurationManager.ConnectionStrings[country].Name)); | |
db.Country = country; | |
db.ObjectMaterialized += new System.Data.Objects.ObjectMaterializedEventHandler(db_ObjectMaterialized); | |
return db; | |
} | |
static void db_ObjectMaterialized(object sender, System.Data.Objects.ObjectMaterializedEventArgs e) | |
{ | |
var db = sender as CountryShardedDatabase; | |
var user = e.Entity as User; | |
if (db != null && user != null) | |
{ | |
user.Country = db.Country; | |
} | |
} | |
public string Country { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment