Entity Framework Questions and Answers 3
۲۱٫ How can we do pessimistic locking in Entity Framework?
We cannot do pessimistic locking using Entity Framework. You can invoke a stored procedure from Entity Framework and do pessimistic locking by setting the isolation level in the stored procedure. But directly, Entity Framework does not support pessimistic locking.
۲۲٫ What is client wins and store wins mode in Entity Framework concurrency?
Client wins and store wins are actions which you would like to take when concurrency happens. In store wins / database wins, the data from the server is loaded into your entity objects. Client wins is opposite to stored wins, data from the entity object is saved to the database. We need to use the Refresh method of the Entity Framework context and provide the RefreshMode enum values. Below is a simple code snippet which executes ClientWins.
۲۳٫ What are scalar and navigation properties in Entity Framework?
Scalar properties are those where actual values are contained in the entities. For example, in the above customer entity, customername and customerid are scalar properties. Normally a scalar property will map to a database field. Navigation properties help to navigate from one entity to another entity. For instance, consider the below example in which we have two entities: Customer and Address, and a customer has multiple address objects. Now we would like to have a facility where at any given moment we would like to browse from a given customer object to the addresses collection and from the address object to the customer. If you open the Entity Designer, you would notice navigation properties as shown below. The navigation properties are automatically created from the primary and foreign key references. So now because of those navigation properties, we can browse from the Customer to the Addresses object, look at the below code:
Customer Cust = oContext.Customers.ToList<Customer>();
// From customer are browsing addresses
List<Address> Addresses = Cust.Addresses.ToList<Address>();
You can also do vice versa. In other words, from the Address object, you can reference the Customer object, as shown in the below code.
Address myAddress = Addresses;
// From address we can browse customer
Customer cus = myAddress.Customer;
۲۴٫ What are complex types in Entity Framework?
There can be situations where you have common properties across entities. For example, consider the below figure where we have Customer and Supplier entities. They have three fields in common: Address1, Address2, and PhoneNo. These fields have been duplicated both in the Customer and Supplier entities. So to remove these duplicate and redundant fields, we can move them to a common complex type called Address. Complex types group common fields so that they can be reused across entities. To create a complex type, select the fields which you want to group in a complex type, click on Refactor, and create the complex type. Below is a figure which shows this. Once the complex type is created, you can then reuse the complex type with other entities as well.
۲۵٫ What’s the difference between LINQ to SQL and Entity Framework?
LINQ to SQL is good for rapid development with SQL Server. EF is for enterprise scenarios and works with SQL Server as well as other databases. LINQ maps directly to tables. One LINQ entity class maps to one table. EF has a conceptual model and that conceptual model maps to the storage model via mappings. So one EF class can map to multiple tables, or one table can map to multiple classes. LINQ is more targeted towards rapid development while EF is for enterprise level where the need is to develop a loosely coupled framework.
۲۶٫ What is the difference between DbContext and ObjectContext?
DbContext is a wrapper around ObjectContext, it’s a simplified version of ObjectContext.
As a developer you can start with DbContext as it’s simple to use. When you feel that some of the operations cannot be achieved by DbContext, you can then access ObjectContext from DbContext, as shown in the below code:
۲۷٫ Encrypt ConnectionString in Web.Config?
Open Command Prompt with Administrator privileges
At the Command Prompt, enter: [cd C:\Windows\Microsoft.NET\ Framework\v4.0.30319]
In case your web Config is located in “D:\Articles\EncryptWebConfig” directory path, then enter the following to encrypt the ConnectionString: [ASPNET_REGIIS -pef “connectionStrings” “D:\Articles\EncryptWebConfig”]
After encrypting your ConnectionStrings section, your ConnectionStrings will not be in a readable format.
It’s very good to know that ASP.NET automatically decrypts the contents of the Web.Config file when it processes the file.
It is possible to decrypt my Web.Config so that I can read it in original format
Simply perform the following command to decrypt the connectionStrings [ASPNET_REGIIS -pdf “connectionStrings” “D:\Articles\EncryptWebConfig”]
۲۸٫ Set the connection string for Entity Framework at runtime?
Read the connection string from web config / app config / Hard coded
Create the object for EntityConnectionStringBuilder and assign the connection string
Create DbContex object for the new connection string
string connectionString = new System.Configuration.ConfigurationSettings. AppSettings[“ConnectionString”]);
System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient. SqlConnectionStringBuilder(connectionString);
EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.Metadata = “res://*/Sample.csdl|res://*/Sample.ssdl| res://*/Sample.msl”;
ecb.Provider = “System.Data.SqlClient”;
ecb.ProviderConnectionString = scsb.ConnectionString;
dataContext = new SampleEntities(ecb.ConnectionString);