Entity Framework Code First : how to annotate a foreign key for a “Default” value?

The problem is that when you have multiple relationships between two entities, EF Code First isn’t able to find out which navigation properties match up, unless, you tell it how, here is the code: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } /****Change Nullable<int> … Read more

Entity Framework 4.1+ many-to-many relationships change tracking

Here is how to find all the changed many-to-many relationships. I’ve implemented the code as extension methods: public static class IaExtensions { public static IEnumerable<Tuple<object, object>> GetAddedRelationships( this DbContext context) { return GetRelationships(context, EntityState.Added, (e, i) => e.CurrentValues[i]); } public static IEnumerable<Tuple<object, object>> GetDeletedRelationships( this DbContext context) { return GetRelationships(context, EntityState.Deleted, (e, i) => e.OriginalValues[i]); … Read more

Entity Framework – Detach and keep related object graph

Try using a NoTracking query instead. That way the objects arenever attached, so you don’t need to ‘detach’ which is when the graph is shredded: i.e. using (var creativeWorkshopEntities = new CreativeWorkshopEntities()) { creativeWorkshopEntities.Job.MergeOption = MergeOption.NoTracking; var q = from c in creativeWorkshopEntities.Job.Include(“Files”) where c.Id == jobId select c; var job = q.First(); return job; … Read more

Entity Framework: DbContext and setting the ProviderName

Create your DbConnection manually and pass it to the DbContext constructor as follows: var conn = DbProviderFactories.GetFactory(“MY_CONN_PROVIDER”).CreateConnection(); conn.ConnectionString = “MY_CONN_STR”; new DbContext(conn, true); Notice the second parameter bool contextOwnsConnection is true. Since you don’t re-use the connection elsewhere, it lets the context manage the connection and Dispose() it when needed.

int to string in Entity Framework

Sadly EF does not know how to convert .ToString(). You must use the embedded function SqlFunctions.StringConvert: http://msdn.microsoft.com/en-us/library/dd466292.aspx Also there is no overload for int so you must typecast to double 🙁 var vendors = from v in Vendors select new { Code = SqlFunctions.StringConvert((double)v.VendorId) };

Convert datetime to a formatted string inside a LINQ-to-entities query

Another option is using SqlFunctions.DateName, your code will be like this: var offer = (from p in dc.CustomerOffer join q in dc.OffersInBranch on p.ID equals q.OfferID where q.BranchID == singleLoc.LocationID let value = (p.OriginalPrice – p.NewPrice) * 100 / p.OriginalPrice orderby value descending select new { Title = p.OfferTitle, Description = p.Description, BestOffer = value, … Read more

Joining tables using more than one column in Linq To Entities

You can write it using two from expressions like below: from a in Table1s from b in Table2s where a.ID1Table1 == b.ID1Table2 && a.ID2Table1 == b.ID2Table2 select new {a.ID1Table1, a.ID2Table1, a.Value1Table1, b.ID3Table2, b.Value1Table2} Using join: from a in Table1s join b in Table2s on new{PropertyName1 = a.ID1Table1, PropertyName2 = a.ID2Table1} equals new{PropertyName1 = b.ID1Table2, PropertyName2 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)