Rename column in entity framework - entity-framework-4

I have a table 'UserLogin' which contains the columns: id and userid. Suppose I want to show userid as 'user' in Gridview then we write query in sql
select userid as user from UserLogin
But I am working with entity framework. How can we solve this problem in entity framework?

Usually you specify different Header text for that column within your GridView control.
But you can achieve similar functionality in Entity framework by using
select new { user = UserLogin.userid, id = UserLogin.id };

Related

Add column to database without defining it in model with Entity Framework

Is it possible to add a column (or execute some SQL) to a table when Entity Framework is instantiating a new database, without defining it in the Model used in DbSet ?
Iam building a prototype for a SaaS application with Entity Framework and Elastic Scale Client, and want to use Row Level Security, for that a need a column to identify my tenants. So i figured it would be nice if I could use just the EF initializer to add this column, when adding new tenants to the system.
As noted in the documentation here, you can execute sql when you have the DBContext.
From the docs:
using (var context = new BloggingContext())
{
context.Database.ExecuteSqlCommand(
"UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1");
}
You could use this to modify the structure without having the actual structure defined in EF.

ASP.NET MVC Model First Many To Many

I have some problems working with model first many to many relationship. Since I created many-many relationship between Town and Author via interface builder it created table TownAuthor with keys Towns_TownID and Authors_AuthorID but I want that just to be called TownID and AuthorID, how do I change that?
In Code first I would use that modelBuilder configuration in Context but I have no idea how to do this via model first...
You have to change the names of these columns in the Entity Designer DDL script (which is generated from the EDMX file and has ModelName.edmx.sql name) before executing it.
-- Creating table 'TownAuthor'
CREATE TABLE [dbo].[TownAuthor] (
[TownID] int NOT NULL,
[AuthorID] int NOT NULL
);
GO

C# mvc How to set AutoIncrement on

Hi I am trying to create a new customer record. The primary id customerid is autoincremental column.
When I try to insert a new record I get this error :
Message=Cannot insert explicit value for identity column in table 'Customer' when IDENTITY_INSERT is set to OFF.
This is the same code I am using :
NewCustomer.Name= user.Name;
NewCustomer.Address= user.Address;
NewCustomer.Phone = user.Phone;
NewCustomer.Email= user.Email;
db.Customers.Add(NewCustomer);
db.SaveChanges();
I read somewhere that I have to set autoincrement on, but I am using entity framework and not any direct sql.
Can some one please help how I could turn on autoincrement so I could insert records?

How to query a list in LINQ

I have 2 tables in my database, one for projects members and one for projects
After authenticating the user I have his ID. In my project members table I have the project ID and the user ID.
I can obtain a list of project members for this user using
var pm = db.ProjectMembers.Where(c=> c.UserID=u.UserID) ;
Now my question is how can I get a list of ProjectID from the above list. And then how can I get a list of projects from Project table using the list of ProjectID.
First part of your question:
var projectIDs = db.ProjectMembers.Where(c=> c.UserID=u.UserID).select(pm=>pm.ProjectID) ;
2nd part:
if you are using Entity Framework, you should see a property called (project) for every entity of your ProjectMembers list, you can simply ignore my first part of this answer and go directly with this one
var projectsList = db.ProjectMembers.Where(c=> c.UserID=u.UserID).select(pm=>pm.Project) ;
the property may have a different name, give it a try and let me know what happened.
note: that if the retrieved projects were null, then your Entity framework is working wth Eager loading (Google it).
so in order to let the Entity framework generate the proper SQL syntax to retrieve the projects data, add an Include() to your query as the following:
var projectsList = db.ProjectMembers.Where(c=> c.UserID=u.UserID).Include("Projects").select(pm=>pm.Project) ;

Concurrency check fails with hierarchical data EF 4.0

I got a problem with Entity Framework 4.0
I have a hierarchical table Category: Id, Name, ParentCategory_Id, timestamp
The "timestamp" field is marked as "Concurrency Mode" = "Fixed"
And I'm using Self-Tracking Entity "Category" to manage Category entity in my MVC application.
The situation:
I create STE "NewCategory",
set field Name='bla-bla'
create new STE "ParentCategory" like this:
var ParentCategory = new Category{Id=45};
ParentCategory.MarkAsUnchanged();
NewCategory.Parent = ParentCategory;
Call ApplyChanges() method in my STE Context and call SaveChanges()
The Query is "Update Category set Name=...." !!!!!!
If I do NewCategory.Parent = null OR set "Concurrncy Mode" = "Node" in model scheme everything works FINE.
How to use hierarchical data with concurrency check ?
I solved this problem.
The solution is that the EF4.0 could manage theese situations only if you will use FK Assotiation properties.
In this sample the right way to do:
1)Create self assotiation in Category entity in edmx model. In "Referential Constraint" create link with parent entity via ParentCategory_Id property.
2) Create new instance of Category STE.
3) Set relation:
NewCategory.ParentCategory_Id = 45;
4) ApplyChanges, SaveChanges.

Resources