EF ICollection Vs List Vs IEnumerable Vs IQueryable

IQueryable: Query isn’t executed until you really iterate over the items, maybe by doing a .ToList() or a foreach. Which means you still can add filters, like a Where(). Extends IEnumerable IEnumerable: Forward-only list of items. You can’t get at “item 4” without passing items 0-3. Read-only list, you can’t add to it or remove … Read more

ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

From a point of view of clean creation of POCO entities, there is no difference between the two generators. Both generators produce the same entities, however, ADO.NET POCO Entity Generator is based on ObjectContext‘s API, whereas ADO.NET DbContext Generator is based on DbContext‘s API. DbContext’s API has a few very nice new features (Local, Query … Read more

“Cannot drop database because it is currently in use”. How to fix?

The problem is that your application probably still holds some connection to the database (or another application holds connection as well). Database cannot be deleted where there is any other opened connection. The first problem can be probably solved by turning connection pooling off (add Pooling=false to your connection string) or clear the pool before … Read more

Unique key with EF code first

Unfortunately you can’t define it as unique key in code first because EF doesn’t support unique keys at all (it is hopefully planned for next major release). What you can do is to create custom database intializer and add unique index manually by calling SQL command: public class MyInitializer : CreateDatabaseIfNotExists<MyContext> { protected override void … Read more

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

If you talk about dynamic proxies in EF there are two different types to distinguish: Proxies for lazy loading Proxies for change tracking Usually a change tracking proxy also can serve as a proxy for lazy loading. The reverse is not true. This is because the requirements for change tracking proxies are higher, especially all … Read more

How to retrieve Data Annotations from code? (programmatically)

Extension method: public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property .GetCustomAttributes(attrType, false).First(); } Code: var name = player.GetAttributeFrom<DisplayAttribute>(nameof(player.PlayerDescription)).Name; var maxLength = player.GetAttributeFrom<MaxLengthAttribute>(nameof(player.PlayerName)).Length;

Better way to query a page of data and get total count in entity framework 4.1?

The following query will get the count and page results in one trip to the database, but if you check the SQL in LINQPad, you’ll see that it’s not very pretty. I can only imagine what it would look like for a more complex query. var query = ctx.People.Where (p => p.Name.StartsWith(“A”)); var page = … Read more

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