如何对两个表使用相同的模型。或者 db 最佳实践
How to use same model for two tables. Or db best practice
在 MVC 应用程序中,我正在设置一个包含多个具有相同结构的 table 的数据库,我想对所有这些应用程序使用相同的模型,而不必为它们创建一个新模型每个。当尝试使用同一模型创建多个 tables 时,出现以下错误:
"Multiple object sets per type are not supported"
是否可以使用相同的模型创建两个 table?
我正在建立一个包含股票历史价格的数据库。这个想法是每个股权table。
Stock1 Stock2
-Date -Date
-Price -Price
或者 db-design 中的最佳实践是将具有相同结构的所有数据放在一个 table 中,然后使用另一个 table 将数据连接到父级?
Instruments Data
-StockId -Date
-StockName -Price
-StockId(FK)
代码:
//Models for tables
public class Instruments
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Equity
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
[ForeignKey("Instruments")]
public int InstrumentId { get; set; }
public virtual Instruments Instruments { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Low { get; set; }
public int Volume { get; set; }
}
//Code to create tables when running "add-migration"
public DbSet<Instruments> Instruments { get; set; }
public DbSet<Equity> Stock1 { get; set; }
public DbSet<Equity> Stock2 { get; set; }
每只股票 Table 是个坏主意。
您可以将所有股票的数据存储在 DbContext class 的 Stock1
属性 中。添加 property/column 以区分股票 name/StockId 和您的股票 table。
如果您正在使用 StockI
,请在您的 Equity
中创建一个 Stock table 和 StockId
table 将有一个到 Stock 的外键连接table.
public class Stock
{
public int Id { get; set; }
public string Ticker { set;get; }
public string Name { get; set; }
}
public class Equity
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
[ForeignKey("Instruments")]
public int InstrumentId { get; set; }
public virtual Instruments Instruments { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Low { get; set; }
public int Volume { get; set; }
public int StockId { set;get;}
public virtual Stock Stock { set;get;}
}
现在在您的 DbContext class 中,您将只有一个 属性 DbSet<Equity>
类型。您可以通过此访问所有股票记录或根据您的需要对其进行过滤(例如:获取特定股票的记录)
public DbSet<Equity> Equities { get; set; }
要获取特定股票的数据,您通常会使用过滤器。例如,
var stockIdOfMsft = 24;
var msftEquities = db.Equities.Where(a=>a.StockId == stockIdOfMsft).ToList();
或
var msftEquities = db.Equities.Where(a=>a.Stock.Ticker == "MSFT").ToList();
在 MVC 应用程序中,我正在设置一个包含多个具有相同结构的 table 的数据库,我想对所有这些应用程序使用相同的模型,而不必为它们创建一个新模型每个。当尝试使用同一模型创建多个 tables 时,出现以下错误:
"Multiple object sets per type are not supported"
是否可以使用相同的模型创建两个 table?
我正在建立一个包含股票历史价格的数据库。这个想法是每个股权table。
Stock1 Stock2
-Date -Date
-Price -Price
或者 db-design 中的最佳实践是将具有相同结构的所有数据放在一个 table 中,然后使用另一个 table 将数据连接到父级?
Instruments Data
-StockId -Date
-StockName -Price
-StockId(FK)
代码:
//Models for tables
public class Instruments
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Equity
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
[ForeignKey("Instruments")]
public int InstrumentId { get; set; }
public virtual Instruments Instruments { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Low { get; set; }
public int Volume { get; set; }
}
//Code to create tables when running "add-migration"
public DbSet<Instruments> Instruments { get; set; }
public DbSet<Equity> Stock1 { get; set; }
public DbSet<Equity> Stock2 { get; set; }
每只股票 Table 是个坏主意。
您可以将所有股票的数据存储在 DbContext class 的 Stock1
属性 中。添加 property/column 以区分股票 name/StockId 和您的股票 table。
如果您正在使用 StockI
,请在您的 Equity
中创建一个 Stock table 和 StockId
table 将有一个到 Stock 的外键连接table.
public class Stock
{
public int Id { get; set; }
public string Ticker { set;get; }
public string Name { get; set; }
}
public class Equity
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
[ForeignKey("Instruments")]
public int InstrumentId { get; set; }
public virtual Instruments Instruments { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Low { get; set; }
public int Volume { get; set; }
public int StockId { set;get;}
public virtual Stock Stock { set;get;}
}
现在在您的 DbContext class 中,您将只有一个 属性 DbSet<Equity>
类型。您可以通过此访问所有股票记录或根据您的需要对其进行过滤(例如:获取特定股票的记录)
public DbSet<Equity> Equities { get; set; }
要获取特定股票的数据,您通常会使用过滤器。例如,
var stockIdOfMsft = 24;
var msftEquities = db.Equities.Where(a=>a.StockId == stockIdOfMsft).ToList();
或
var msftEquities = db.Equities.Where(a=>a.Stock.Ticker == "MSFT").ToList();