Tuesday, January 22, 2013

Adding Custom Propeties to EF 4.0 that doesn't exist in the DB model

Hi Folks,

I was extending my EF 4.0 model by having custom propeties that don't exist in my DB, Since EF 4.0 doesn't support adding custom properties if it doesn't exist in the DB, I found a workaround for that by creating a partial class with my custom properties in it.
 

public partial class myEntityName: EntityObject

{

     
[DataMemberAttribute()]
public string customerName{ get; set; }

[DataMemberAttribute()]
public int customerCode{ get; set; }

}


Implementing this workaround will help you to add any properties you would like to add in your EF 4.0 entities.


You might encounter this error since you are adding properties to your entity that doesn't exist in the edm file:

"The number of members in the conceptual type does not match with the number of members on the object side type .Make sure the number of members are the same."

The fix for error; Remove "EdmScalarPropertyAttribute" attribute from your custom propeties:

//[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)] 
[DataMemberAttribute()]
public string customerName{ get; set; }


 Hope this helps!

Wednesday, January 02, 2013

WCF Error: The service cannot be activated because it does not support ASP.NET compatibility.

Hi Folks,

I was developing a secure WCF service that allows only authenticated windows users to access this service in IIS 7.5. After disabling the anonymous access i figured that wsHttpBinding will not work because you have to enable it to make the WCF binding working, then i changed the WCF binding to basicHttpBinding and passed the user credentials from the web application by using below line of code:

C# Code:
svc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

The above line passed the user credentials from the client web application to the service.

Passing the credentials from the client web application wasn't accepted by the WCF service because i was getting another error message from the wcf service:

"The underlying provider failed on Open WCF when hosting in IIS with impersonation is enabled"

Since i have enabled the impersonation to pass the user credentials from the web application to the wcf i was missing the step to add the following configuration in the web.config of the WCF service:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

"The default is false. When this attribute is set to true, requests to Windows Communication Foundation (WCF) services flow through the ASP.NET HTTP pipeline, and communication over non-HTTP protocols is prohibited"

One last thing you should add in the service class "aspNetCompatibilityEnabled" attribute to comply with configuration entry otherwise still your service won't work because it will use the default IIS account to access your database.

C# Code:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class myService : ImyService
// your implementation to the service.
}


Hope this helps!

References:

1) WCF serviceHostingEnvironment element:
http://msdn.microsoft.com/en-us/library/ms731336(v=VS.110).aspx