I am referring Entity Framework Tutorial from that I get to know that when we accessing database into the framework it automatically creates the entity for all the tables which we want to use.
So here my question is can I use my own created class file of a table instead of using auto generated entity and is that a good practice or I need to use only that entities as a good practice.
Please tell me which should be a good way.
E.g. if I have a Database StudentInfo
in which having Student
table so if I Access it into my project using EF. it will create Entity for Student by default. but if I again created my own class file for Student that will be good way or need to use default generated entity.

- 22,727
- 9
- 68
- 113
-
Can you show us some code you created and/or a link to the tutorial you are referring to? – Christoph Fink Apr 03 '18 at 08:44
-
http://www.entityframeworktutorial.net/what-is-entityframework.aspx – Apr 03 '18 at 08:45
-
I think the term you are looking for is "ViewModels". Here's a related question to read up upon: https://stackoverflow.com/questions/9881790/how-to-design-viewmodel – Marco Apr 03 '18 at 08:48
-
1Or this: http://www.entityframeworktutorial.net/choosing-development-approach-with-entity-framework.aspx – Christoph Fink Apr 03 '18 at 08:48
-
2check this link https://cpratt.co/entity-framework-code-first-with-existing-database/ – Jophy job Apr 03 '18 at 08:52
3 Answers
I understand you are using a Database First approach, so you are importing the tables into your project and Visual Studio is generating the code related to your Entities, DBContext, etc... In that case, you must use those entity classes as the DBContext refers to them.
You have some control about the generated code, as you can change class/attribute names, types, etc... and you can also extend those generated classes as they are partial classes.
In any case, if you want to use your own classes, you should follow a Code First approach. This will require more work to define all the mappings with the DB model, but definitely you will be able to use your own classes.

- 718
- 1
- 6
- 25
-
okk. but suppose in my Student table I am having 10 columns and i want to query only for specific columns then still i need to take all column in the query. or otherwise if I created my own class file in that I should mentioned only those columns which I wanted from my Student table – Apr 03 '18 at 09:37
-
If you don't want to use those columns at all, you can remove them in the mapping. If you want to define some kind of simplified Entity, then we are talking about a different topic, a ViewModel mentioned above or a DTO. You probably want to keep the full entity class generated and project/map into that viewmodel class. – agascon Apr 03 '18 at 09:46
-
okk. Thanks a lot for your valuabe time. Its good to use entity classes rather than own created – Apr 03 '18 at 09:50
-
Well, probably the point is that they way your are mapping the DB it's the only option, which doesn't mean using your own classes for the entities was a bad practice. Have a look on EF Code First approach, each one has its pros and cons. I'm pretty sure there tons of discussions in SO about this and you will find opinions supporting each of them. – agascon Apr 03 '18 at 10:11
-
I have a Existing Database and I access it using EF in my Project but still for querying I am using my own created model class file thats why I get confused is that correct or not. Because I am creating an API services from my tables. – Apr 03 '18 at 10:16
-
-
Absolutely. You can map you query results to any other classes, which could be DTO classes. If you application is large, you could be interesting on library which could automate this mapping, like AutoMapper. – agascon Apr 03 '18 at 11:28
-
If you create your own classes instead of the ones generated by the EF, you'll have to define some kind of mappings through which the framework understands that your custom class represents the DB entity itself.
Hence It would make sense to use the entities generated by the EF, and create your own custom viewmodels over these entities to use in your applications.

- 11
- 4
-
Instead of using Viewmodels , if I use DTO then? taking only selected column in that DTO class file – Apr 03 '18 at 11:11
-
DTOs too perfectly makes sense, which would have only those columns or properties that you exactly require. You can query your DB context to return you the desired columns and then can map those columns to your DTO properties. – anantverma_ Apr 03 '18 at 11:24
well what ever you want to tell what i understand is that u want to use your own generated classes .and that is a good practice you should try make every thing your own .main issue of the entity farm work is that when it creates any class for a table it crests it on the main directory of the project and named as i think EDMX and still you have to do lot of works too . instead using entity framework just make the classes .and if you want to update specific columns of a table just run the update query with the specific column names . here i have make a function that will classes for all your tables just change the connection string in the code and enjoy..............this function will return a string just copy and past this to your classes class and enjoy .always try to make your own .........using this type of programming even you can make pages too..........yes
public string makeclassandfunctions(string tablename)
{
DataSet ds = loadtabledata(tablename);
//the class starts frome here
string classoftable = "</br></br></br>public class " + tablename + "</br>{ " + "</br>";
for (int a = 0; a <= ds.Tables[0].Columns.Count-1 ; a++)
{
if (ds.Tables[0].Columns[a].ColumnName.Contains("ID"))
{
classoftable += "public Int32 " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
}
else
{
classoftable += "public string " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
}
}
//class is ending here its variables are defined here
classoftable += "} ";
//the insertion function starts frome here
classoftable += "</br>public void insertinto" + tablename + "(" + tablename + " obj" + tablename + ")</br>{ </br> ";
classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" insert into " + tablename + " values " + "(";
for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
{
if (a == ds.Tables[0].Columns.Count - 1)
{
classoftable += "@" + ds.Tables[0].Columns[a].ColumnName;
}
else
{
classoftable += "@" + ds.Tables[0].Columns[a].ColumnName + " , ";
}
}
classoftable += ")" + @""",sqlcon);</br>";
for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
{
classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
}
classoftable += " cmd.ExecuteNonQuery();</br>sqlcon.Close();";
classoftable += "}</br></br>";
//insert function ends here
//delete function startsfrome here
classoftable += "public void deletefrom" + tablename + "(" + "Int32 id" + ")" + "</br>{ </br> ";
classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" delete from [" + tablename + "]" + " where " + ds.Tables[0].Columns[0].ColumnName + "= " + @"'" + @"""" + "+id+" + @"""" + @"'" + @"""" + ",sqlcon);" + "</br> cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br></br>";
//delete ends here
//updatefunctionstarts frome here
classoftable += "public void update" + tablename + "(" + tablename + " obj" + tablename + " )</br>{";
classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" update [" + tablename + "] set ";
for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
{
string columnname = ds.Tables[0].Columns[a].ColumnName;
if (a == ds.Tables[0].Columns.Count - 1)
{
classoftable += columnname + " = @" + columnname + " ";
classoftable += "where " + ds.Tables[0].Columns[0].ColumnName + " = @" + ds.Tables[0].Columns[0].ColumnName;
}
else
{
classoftable += columnname + " = @" + columnname + " , ";
}
}
classoftable += @"""" + ",sqlcon);</br>";
for (int a = 0; a <= ds.Tables[0].Columns.Count - 1; a++)
{
classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
}
classoftable += "cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br>";
//updatesstatementfunction ends here
return classoftable;
}

- 1
- 5