如何使用具有相同列的不同实体EF的唯一C#代码 [英] How to have a unique C# code with different Entities EF having the same columns
问题描述
大家早上好
我正在使用 ASP.NET-MVC , C#, EF 和一个 SQL Express 数据库。
I am doing a AngularJS project using ASP.NET-MVC, C#, EF and an SQL Express DB.
我有一个 HTML 页面,其中一些AngularJS函数在 MyController.cs
上调用某些函数。
I have an HTML page calling some AngularJS functions calling some functions on MyController.cs
.
在 MyController.cs
使用 EF 有很多功能。
在数据库中,我有数百个具有相同列的表。
In my DB, I have hundred of tables with the same columns.
我希望每个表具有相同的HTML页面,因此执行具有不同名称的相同功能
And I want to have the same HTML page for each table, so executing the same functions with different names
例如,当我转到链接 http时://..../Index/TABLE1
, MyController.cs
看起来像是:
For example when I go to the link http://..../Index/TABLE1
, MyController.cs
would look like :
public ActionResult getCaptions()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = 500000000;
var data =
_db
.TABLE1
.OrderBy(i => i.CodeId)
.ToArray();
return Content(serializer.Serialize(data).ToString(), "application/json");
}
,当我转到链接 http:// ... ./Index/TABLE2
, MyController.cs
看起来像是:
and when I go to the link http://..../Index/TABLE2
, MyController.cs
would look like :
public ActionResult getCaptions()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = 500000000;
var data =
_db
.TABLE2
.OrderBy(i => i.CodeId)
.ToArray();
return Content(serializer.Serialize(data).ToString(), "application/json");
}
我已经考虑过这样的解决方案:
I have thought about a solution like this :
在控制器上声明全局变量
tableName
修改索引ActionResult
Modify the Index ActionResult
public ActionResult Index(string id)
{
tableName = id;
return View();
}
现在我被卡住了...
Now I am stuck ...
需要任何帮助,谢谢。
编辑:如果您投反对票,您至少可以解释一下原因吗?谢谢
推荐答案
我看到你@aBennouna可能已经放弃了这个问题,但是我对这个问题,并决定需要一个解决方案。可能不是您要求的100%,因为在这里您不能将表名称作为字符串参数。
I see you @aBennouna may have already given up on this question, but I took an interest in the question and decided it needed a solution. It may not be 100% what you asked for since here you can't take in a table name as a string parameter.
首先,如果所有表都具有相同的列,您可以从同一基础继承它们:
First if all the tables have same columns, you can inherit them from a same base:
public class TableBase
{
[Key]
public int CodeId { get; set; }
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
}
使用此基础,我们可以定义表:
Using this base we can define our tables:
public class Table1 : TableBase
{
}
public class Table2 : TableBase
{
}
// etc...
此使我们能够编写通用的 GetCaptions 方法,如@CodeCaster建议的(感谢朝着正确方向的微移):
This enables us to write a generic GetCaptions method like @CodeCaster suggested (thanks for the nudge in right direction):
public ActionResult GetCaptions<T>() where T : TableBase
{
var set = db.Set<T>();
// get all objects to array
var list = set.OrderBy(i => i.CodeId).ToList();
// serialize and return result
// OR get single object and a value
var item = set.FirstOrDefault();
var propertyValue = item.Prop1;
}
用法:
GetCaptions<Table1>();
GetCaptions<Table2>();
这篇关于如何使用具有相同列的不同实体EF的唯一C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!