Viva-Entity Framework

1.       What is Entity Framework?
Entity Framework is an additional layer between application and database that enables the developers to program against the conceptual application model instead of programming directly against the relational storage schema.
2.       Will there be any issues adding a table without primary keys to a data model?
Every entity must have a key, even in the case where the entity maps to a view. When you use the Entity Designer to create or update a model, the classes that are generated inherit from EntityObject, which requires EntityKey. So, we have to have a primary key in the table to add it to the data model.
3.       How do you truncate a table using entity data model?
Unfortunately Entity Framework doesn't include anything straight forward to handle this. But we can still call a T-SQL statement using entity framework that will still minimizes the developers work. We can call ExecuteStoreCommand() methond on ObjectContext as shown below.
using (var context = new MyTestDbEntities())
{
    context.ExecuteStoreCommand("TRUNCATE table Dummy");
}

4.       How do you query in entity model when the result has a join from from different database other than the entity model? E.g.: SELECT t1.c1, t2.c2 FROM table1 AS t1 JOIN table2 t2 ON t1.c1 = t2.c1
As the entity model doesn't support querying from any entity other than the entities defined in Entity Data Model, we have to query aginst the data base using ExecuteStoredQuery of the context.
Following code snippet shows how to query when other databases are joined.
string query = "SELECT t1.c1, t2.c2 FROM table1 AS t1 JOIN table2 t2 ON t1.c1 = t2.c1";
using (var context = new SampleEntities())
{
  ObjectResult<DbDataRecord> records = context.ExecuteStoreQuery<DbDataRecord>(query);
  foreach (DbDataRecord record in records)
  {
    //Do whatever you want
  }
}

5.       What is minimum requirement for Entity Framework applications to run?
The Entity Framework is a component of the .NET Framework so EntityFramework applications can run on any computer on which the .NETFramework starting with version 3.5 SP1 is installed.
6.       What is CSDL?
Conceptual schema definition language (CSDL) is an XML-based language that describes the entities, relationships, and functions that make up a conceptual model of a data-driven application. This conceptual model can be used by the Entity Framework or WCF Data Services.The metadata that is described with CSDL is used by the EntityFramework to map entities and relationships that are defined in a conceptual model to a data source.More=> http://msdn.microsoft.com/en-us/library/bb399292.aspx
7.       What is SSDL?
Store schema definition language (SSDL) is an XML-based language that describes the storage model of an Entity Framework application. In an Entity Framework application, storage model metadata is loaded from a .ssdl file (written in SSDL)into an instance of the System.Data.Metadata.Edm.StoreItemCollection and is accessible by usingmethods in the System.Data.Metadata.Edm.MetadataWorkspace class. The Entity Framework usesstorage model metadata to translate queries against the conceptual model to store-specific commands
8.       What is MSL?
Mapping specification language (MSL) is an XML-based language that describes the mapping between the conceptual model and storage model of an Entity Framework application.In an Entity Framework application, mapping metadata is loaded from an .msl file (written in MSL) at buildtime. The Entity Framework uses mapping metadata at runtime to translate queries against the conceptual model to store-specific commands
9.       What is Entity Data Model?
The Entity Data Model (EDM) is a set of concepts that describe the structure of data, regardless of it's stored form. The EDM borrows from the Entity-Relationship Model described by Peter Chen in 1976, but it also builds on the Entity-Relationship Model and extends its traditional uses.The EDM addresses the challenges that arise from having data stored in many forms. For example,consider a business that stores data in relational databases, text files, XML files, spreadsheets, and reports. This presents significant challenges in data modeling, application design, and data access. When designing a data-oriented application, the challenge is to write efficient and maintainable code without sacrificing efficient data access, storage, and scalability. When data has a relational structure, data access, storage, and scalability are very efficient, but writing efficient and maintainable code becomes more difficult. When data has an object structure, the trade-offs are reversed: Writing efficient and maintainable code comes at the cost of efficient data access, storage, and scalability. Even if the right balance between these trade-offs can be found, new challenges arise when data is moved from one form to another. The Entity Data Model addresses these challenges by describing the structure of data in terms of entities and relationships that are independent of any storage schema. This makes the stored form of data irrelevant to application design and development. And, because entities and relationships describe the structure of data as it is used in an application (not its stored form), they can evolve as an application evolves.
10.   Which are the key concepts of Entity Data Model?
The Entity Data Model (EDM) uses three key concepts to describe the structure of data: entity type,association type, and property. These are the most important concepts in describing the structure of data in any implementation of the EDM.
Entity Type: The entity type is the fundamental building block for describing the structure of data with the Entity Data Model. In a conceptual model, entity types are constructed from properties and describe the structure of top-level concepts, such as a customers and orders in a business application.
Association Type: An association type (also called an association) is the fundamental building block for describing relationships in the Entity Data Model. In a conceptual model, an association represents a relationship between two entity types (such as Customer and Order).
Property Entity types contain properties that define their structure and characteristics. For example, a Customer entity type may have properties such as CustomerId, Name, and Address

11.   What is .edmx file and what it contains?
An .edmx file is an XML file that defines a conceptual model, a storage model, and the mapping betweenthese models. An .edmx file also contains information that is used by the ADO.NET Entity Data ModelDesigner (Entity Designer) to render a model graphically.
12.   How can you tell EF to have a different table or column name than that defined for the class?
By convention, EF defines the table and column names based on your class and property names.You can use the [Table] and [Column] annotations to tell EF to use different names.
13.   How do you mark a property as required? For example, For a Project, the Name is a required field.
You use the [Required] attribute to mark a property as required.
14.   What is use of EntityDataSource Control?
The ADO.NET EntityDataSource control supports data binding scenarios in Web applications that use the ADO.NET Entity Framework. Like the Entity Framework, the control is available as part of the .NETFramework 3.5, beginning with SP1. Like the other Web server data source controls, theEntityDataSource control manages create, read, update, and delete operations against a data source on behalf of data-bound controls on the same page. The EntityDataSource works with editable grids, forms with user-controlled sorting and filtering, dually bound drop-down list controls, and master-detail pages

15.   What is Model First Approach?
A new Model First approach was supported in Visual Studio 2010, which was released together with the second Entity Framework version (Entity Framework v4). In Model First approach the development starts from scratch. At first, the conceptual model is created with Entity Data Model Designer, entities and relations are added to the model, but mapping is not created. After this Generate Database Wizard is used to generate storage (SSDL) and mapping (MSL) parts from the conceptual part of the model and save them to the edmx file. Then the wizard generates DDL script for creating database (tables and foreign keys)If the model was modified, the Generate Database Wizard should be used again to keep the model and the database consistent. In such case, the generated DDL script contains DROP statements for tables, corresponding to old SSDL from the .edmx file, and CREATE statements for tables, corresponding to new SSDL, generated by the wizard from the conceptual part. In Model First approach developer should not edit storage part or customize mapping, because they will be re-generated each time when GenerateDatabase Wizard is launched.
16.   What is Code First Approach?
Code First allows you to define your model using C# or VB.Net classes, optionally additional configuration can be performed using attributes on your classes and properties or by using a Fluent API. Your model can be used to generate a database schema or to map to an existing database.
17.   What is Entity SQL?
Entity SQL is a SQL-like storage-independent language, designed to query and manipulate rich object graphs of objects based on the Entity Data Model (EDM).
18.   What is LINQ To Entities?
LINQ to Entities provides Language-Integrated Query (LINQ) support for querying entities. LINQ to Entities enables developers to write queries against the database using one of the supported.NET Framework programming languages such as Visual Basic or Visual C#
19.   What is EntityClient?
System.Data.EntityClient is a storage-independent ADO.NET data provider that contains classes such as EntityConnection, EntityCommand, and EntityDataReader. Works with Entity SQL and connects to storage specific ADO.NET data providers, such as SqlClient.
20.   What is Deferred Loading(Lazy Loading)?
When objects are returned by a query, related objects are not loaded at the same time.Instead they are loaded automatically when the navigation property is accessed. Also known as “lazyloading,
21.   What is Eager Loading?
The process of loading a specific set of related objects along with the objects that were explicitly requested in the query.
22.   What is Complex Type?
A .NET Framework class that represents a complex property as defined in the conceptual model. Complex types enable scalar properties to be organized within entities. Complex objects are instances of complex types
23.   What is Conceptual Model?
An implementation of the Entity Data Model (EDM), specific to the Entity Framework, which represents an abstract specification for the data structures that define an entity-relationship representation of data in the domain of an application
24.   What is use of Entity Container?
Specifies entity sets and association sets that will be implemented in a specified namespace.
25.   What is Explicit Loading?
When objects are returned by a query, related objects are not loaded at the same time. By default, they are not loaded until explicitly requested using the Load method on a navigation property
26.   What do you mean by Navigation Property?
A property of an entity type that represents a relationship to another entity type, as defined by an association. Navigation properties are used to return related objects as an EntityCollection or an EntityReference, depending on the multiplicity at the other end of the association
27.   What is scalar property?
A property of an entity that maps to a single field in the storage model.
28.   What is split entity?
An entity type that is mapped to two separate types in the storage model
29.   What do you mean by table-per-hierarchy?
A method of modeling a type hierarchy in a database that includes the attributes of all the types in the hierarchy in one table
30.   What do you mean by table-per-type?
A method of modeling a type hierarchy in a database that uses multiple tables with one-to-one relationships to model the various types.
How does LINQ to SQL differ from 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 map to 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 loosely coupled framework. 

int customerIdToFind = 1; var customer = context.Customers.FirstOrDefault(c => c.Id == customerIdToFind);


string firstNameToFind = "John"; string lastNameToFind = "Doe"; var customer = context.Customers.FirstOrDefault(c => c.FirstName == firstNameToFind && c.LastName == lastNameToFind);
var customer = context.Customers.FirstOrDefault(c => c.TotalPurchaseAmount > 1000 && c.PremiumStatus);


// Define the parameters for your query string firstName = "John"; string lastName = "Doe"; // Query customers based on the parameters var customers = context.Customers .Where(c => c.FirstName == firstName && c.LastName == lastName) .ToList(); foreach (var customer in customers) { Console.WriteLine($"Customer ID: {customer.Id}");
            }-----------------------------------

var customers = context.Customers .Where(c => c.FirstName == "John") .ToList();------------------------------
var customers = context.Customers .Where(c => c.FirstName == "John" && c.LastName == "Doe") .ToList();---------------------------------------------

var customers = context.Customers .Where(c => c.FirstName == "John" || c.LastName == "Doe") .ToList();-------------------------------------------
var searchTerm = "John"; var customers = context.Customers .Where(c => c.FirstName.Contains(searchTerm) || c.LastName.Contains(searchTerm)) .ToList();

var startDate = new DateTime(2022, 1, 1); var endDate = new DateTime(2022, 12, 31); var customers = context.Customers .Where(c => c.RegisteredDate >= startDate && c.RegisteredDate <= endDate) .ToList();

Edit//Add
 
using (var context = new CustomerContext()) { // Step 1: Check if the customer exists (e.g., by email) string emailToCheck = "john.doe@example.com"; var existingCustomer = context.Customers.FirstOrDefault(c => c.Email == emailToCheck); if (existingCustomer != null) { // Step 2: Edit the existing customer existingCustomer.FirstName = "UpdatedFirstName"; existingCustomer.LastName = "UpdatedLastName"; // You can edit other properties here as needed // Save changes to the database context.SaveChanges(); Console.WriteLine("Customer updated successfully."); } else { // Step 3: Add a new customer since it doesn't exist var newCustomer = new Customer { FirstName = "John", LastName = "Doe", Email = "john.doe@example.com", // Set other properties for the new customer }; context.Customers.Add(newCustomer); // Save changes to the database context.SaveChanges(); Console.WriteLine("New customer added successfully."); } }



Delete
int customerIdToDelete = 1; var customerToDelete = context.Customers.FirstOrDefault(c => c.Id == customerIdToDelete); if (customerToDelete != null) { // Step 2: Remove the customer from the database context context.Customers.Remove(customerToDelete); // Step 3: Save changes to the database context.SaveChanges(); Console.WriteLine("Customer deleted successfully."); } else { Console.WriteLine("Customer not found."); }





http://localhost:5142/api/Hose21mFittingVolume/index http://localhost:5142/api/tbl_Rev_per_Material/index http://localhost:5142/api/tbl_Rev_per_Material/index?pageno=3


Comments

Popular posts from this blog

Travel RESUME CV

Tablig