如何创建一个代码优先模型,在C#MVC中两次引用另一个模型?
我试图理解如何使用代码优先的方法来实现这一点。当我以前用SQL语句和ASP做更多的代码时,我就知道怎么做了。现在,我试图用C#中的代码优先MVC方法来教自己如何理解它 首先,如果我要解释我在SQL中想要什么,我希望复制类似这样的内容如何创建一个代码优先模型,在C#MVC中两次引用另一个模型?,c#,asp.net-mvc,entity-framework,code-first,C#,Asp.net Mvc,Entity Framework,Code First,我试图理解如何使用代码优先的方法来实现这一点。当我以前用SQL语句和ASP做更多的代码时,我就知道怎么做了。现在,我试图用C#中的代码优先MVC方法来教自己如何理解它 首先,如果我要解释我在SQL中想要什么,我希望复制类似这样的内容 Select s1.systemname, s2.systemname, j,jumplaneid from systems s1, systems s2, jumplanes j where s1.systemid = j.system
Select s1.systemname, s2.systemname, j,jumplaneid
from systems s1, systems s2, jumplanes j
where s1.systemid = j.system1id and s2.systemid = j.system2id
这是我的系统模型
public class system
{
public virtual int systemid { get; set; }
public virtual string systemname { get; set; }
public virtual int? posx { get; set; }
public virtual int? posy { get; set; }
public virtual int? starluminosityid { get; set; }
public virtual int? stellartypeid { get; set; }
public virtual int? starspectralclassid { get; set; }
public virtual int? carry { get; set; }
public virtual int? raw { get; set; }
public virtual int? bio { get; set; }
public virtual int? jumppoints { get; set; }
public virtual int? specialrolls { get; set; }
public virtual ICollection<settlement> settlements { get; set; }
public virtual ICollection<fleet> fleets { get; set; }
public virtual ICollection<Jumplane> jumplanes { get; set; }
public virtual stellartype Stellartype { get; set; }
public virtual starluminosity Luminosity { get; set; }
public virtual starspectralclass SpectralClass { get; set; }
}
如果我先设置数据库,我知道该怎么做,但我正在尝试学习代码优先的方法。有谁更熟悉软件开发的最新趋势,能给我指点方向吗
谢谢
更新:感谢你们两位,从你们每个人身上都学到了一些东西。
这就是我的结局
public class Jumplane
{
[Key]
public int jumplaneid { get; set; }
[Range (1,1000)]
public decimal jumpcost { get; set; }
[Required]
[StringLength(30)]
public string jumplanename { get; set; }
public virtual Starsystem starsystem1 { get; set; }
public virtual Starsystem starsystem2 { get; set; }
public virtual ICollection<Supplyline> supplylines { get; set; }
}
公共级跳车道
{
[关键]
公共整数jumplaneid{get;set;}
[射程(11000)]
公共成本{get;set;}
[必需]
[行政长官(30)]
公共字符串jumplanename{get;set;}
公共虚拟Starsystem starsystem1{get;set;}
公共虚拟Starsystem starsystem2{get;set;}
公共虚拟ICollection供应线{get;set;}
}
及
公共类Starsystem
{
[关键]
public int starsystemid{get;set;}
公共字符串systemname{get;set;}
公共int?posx{get;set;}
公共int?posy{get;set;}
公共int?starluminosityid{get;set;}
公共int?stellartypeid{get;set;}
公共int?starspectralclassid{get;set;}
公共整数?进位{get;set;}
公共int?原始{get;set;}
公共int?bio{get;set;}
公共int?跳转点{get;set;}
公共int?特殊rolls{get;set;}
公共虚拟ICollection{get;set;}
公共虚拟ICollection震源组{get;set;}
公共虚拟ICollection跨通道{get;set;}
公共虚拟stellartype stellartype{get;set;}
公共虚拟星光亮度{get;set;}
公共虚拟starspectralclass SpectralClass{get;set;}
}
我将返回并捕获附加注释。因为我的头已经被尖尖的头发盖住了,我错过了注释的意义
当您建议这是一个重命名问题时,我对更新数据库的问题变得很清楚。添加-Verbose选项让我非常清楚。我可能作弊了,但我简化了模型,删除了模型中的两个starsystem引用,更新了数据库,然后逐个添加回去
我也很欣赏关于类资本化和“virtual”关键字使用的代码审查。我确实在id和姓名上留下了更具描述性的前缀,但这主要是为了我自己,因为如果我不使用它们,以后可能会感到困惑。因为代码只针对我,所以我决定传递这条建议,但反馈仍然是最受欢迎的
再次感谢你们两位的时间和专业知识。类似于您在EF中所需的查询,类似于
dbContext.Jumplanes.Include(“system1”).Include(“system2”)
话虽如此,您的模型没有属性/注释,所有内容都是虚拟的。当您希望延迟加载某个属性时,只需将其设置为虚拟。元素的主键不应该真正被延迟加载
查看
Key
和ForeignKey
属性,用这些属性注释您的模型,在不需要的地方删除虚拟关键字,然后让我们知道它是如何运行的:)当您尝试重命名表时,通常会产生此错误
我建议您创建模型,并尝试使用尽可能少的字段实现所需的结果。此外,您希望仅对外部实体使用virtual
关键字
我刚刚测试了一些类似的东西(Admin,AdminLog[包含Admin1和Admin2]),它工作得很好
您应该尝试的代码
现在,这段代码可以工作了,您可以执行所需的LINQ查询了,只需将所有其他字段添加到其中即可
一些指针(可能是主观的)
- 仅限外国用户使用
- 课程以大写字母开头
- 不需要
,SystemID
字段,只需SystemName
和ID
即可,因为您将看到它属于Name
System
PM> update-database -force
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Applying automatic migration: 201302030335260_AutomaticMigration.
System.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:c4c1ba62-806b-4e68-bb0b-161da4d9e23e
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
public class Jumplane
{
[Key]
public int jumplaneid { get; set; }
[Range (1,1000)]
public decimal jumpcost { get; set; }
[Required]
[StringLength(30)]
public string jumplanename { get; set; }
public virtual Starsystem starsystem1 { get; set; }
public virtual Starsystem starsystem2 { get; set; }
public virtual ICollection<Supplyline> supplylines { get; set; }
}
public class Starsystem
{
[Key]
public int starsystemid { get; set; }
public string systemname { get; set; }
public int? posx { get; set; }
public int? posy { get; set; }
public int? starluminosityid { get; set; }
public int? stellartypeid { get; set; }
public int? starspectralclassid { get; set; }
public int? carry { get; set; }
public int? raw { get; set; }
public int? bio { get; set; }
public int? jumppoints { get; set; }
public int? specialrolls { get; set; }
public virtual ICollection<settlement> settlements { get; set; }
public virtual ICollection<fleet> fleets { get; set; }
public virtual ICollection<Jumplane> jumplanes { get; set; }
public virtual stellartype Stellartype { get; set; }
public virtual starluminosity Luminosity { get; set; }
public virtual starspectralclass SpectralClass { get; set; }
}
public class System
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Jumplane
{
public int ID { get; set; }
public virtual System System1 { get; set; }
public virtual System System2 { get; set; }
}