- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 CodeFirst 脚手架 ASP.NET MVC 站点中使用了以下模型(简化):
Public Tax
{
public int ID {get; set; }
public string Name {get; set; }
public decimal Rate {get; set; }
}
Public OrderLine
{
public int ID {get; set; }
public int TaxID {get; set; }
public virtual Tax Tax {get; set; }
public decimal Quantity {get; set; }
public decimal Price {get; set; }
[ScaffoldColumn(false)]
public decimal Amount
{
get { return Quantity * Price; }
set { }
}
[ScaffoldColumn(false)]
public decimal AmountWithTax
{
get { return Amount + (Amount * Tax.Rate / 100); }
set { }
}
}
Amount
和 AmountWithTax
是计算属性,我不需要在 UI 中使用它们,但我需要将它们保存在数据库中。问题是,当我创建一个新的 OrderLine
时,属性 Tax
为空(TaxID
已填充并包含 int 值)所以 AmountWithTax
抛出异常。
如何访问 Rate
属性?
重现步骤:
将这两个模型添加到标准的 ASP.NET MVC EF 项目中,使用右键单击创建脚手架项 -> 添加 > 新建脚手架项... -> MVC5 ... 使用 EntityFramework -> 并在模型中选择模型类。
Visual Studio 将生成 Controller 和 View ,如果 OrderLine 由 Create View 创建,则会在属性 Tax.Rate 上抛出 NullReferenceException
,因为 Tax 为 null
最佳答案
您应该使用 ViewModel,而不是实体模型。使用将您想要的属性映射到实体模型的 View 模型应该会为您修复它。
如果您不想这样,您可以按照 How do you exclude properties from binding when calling UpdateModel()? 中的说明排除属性的绑定(bind)。 :
[Bind(Exclude="Amount,AmountWithTax")]
这将使模型绑定(bind)器忽略这些属性,导致它们的 set
不被调用。
顺便说一句,Amount
和 AmountWithTax
的 setter 将导致 StackOverflowException
,因为它们正在为自己赋值。您也可以通过简单地删除 set
将它们设置为只读。
关于c# - 另一个虚拟属性的 CodeFirst 访问属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023054/
我在带有 SQL Server 数据库的 .NET Core 项目中使用代码优先迁移。但是由于自己解封,我的同事不得不手动更改数据库表。有没有一种优雅的方法可以将迁移与数据库同步,并在手动干预后继续使
我正在尝试创建一个具有 Entity Framework 代码优先环境的数据库。数据库中的表是身份表。 问题出在 SQL Server 的数据库创建上。当我运行应用程序时没有创建数据库。 但是,我可以
我尝试使用 SQLite 为 Entity Framwork 运行 CodeFirst 示例。NuGet 包 SQLite.CodeFirst 已安装并运行无误但它不会创建 SQLite 数据库。这是
我遇到“一个IEntityChangeTracker的多个实例无法引用一个实体对象”问题。经过一番检查之后,看来我有一个正在跟踪更改的对象。问题是我不知道问题对象的来源……它显然已经放到上下文中,但是
我为我的应用程序支持的每种数据库类型创建了过程。并添加到他们的迁移文件中。 我可以在我的代码优先应用程序中调用存储过程MSSQL,就像这两种类型一个 worker.StoredProcedures.E
我搜索了很多但都失败了...我正在尝试创建两个表,一个具有另一个的外键(具体来说,Url_Entries 的 url 是 WordCount 类)。但是当我使用代码优先将对象添加到数据库时,我遇到了异
我在 CodeFirst 脚手架 ASP.NET MVC 站点中使用了以下模型(简化): Public Tax { public int ID {get; set; } public st
我正在使用带有 SQLite 数据库的 WPF 应用程序。尝试使用 SQLite.CodeFirst 以代码优先的方式设计数据库。 错误是:{“创建模型时无法使用上下文。如果在OnModelCreat
该表未在数据库中按应命名的方式命名。这是上下文: 应用数据库上下文: public IDbSet Artwork { get; set; } 在数据库中,该表应命名为 Artwork,但它没有命名为
我有一个看起来像这样的类电子邮件: public class Email { [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
我正在使用 ASP.net-MVC 开发一个项目,由于它是模型优先,它会自动生成数据库的 .mdf 和 .ldf 文件。数据库是2014版本的。当我想将其上传到主机服务器上时,他们告诉我他们不支持20
我想将一个属性用作两个独立实体的外键 - 一次作为复合外键的一部分,一次作为单个外键。这是我的意思的一个简单示例: public class ParentEntity{ [Key, Colum
假设您有一个带有订单状态的订单类,我想在 OrderStatus 类中声明 OrderStatusId。但是,默认情况下没有设置外键关系。如果我在列上使用 [ForeignKey] 属性,它似乎需要一
考虑我有以下类(class); 产品类别 鱼制品 非鱼产品 一个 ProductCategory 可以有许多 FishProducts 和/或许多 NonFishProducts,这是一种非常简单的一
好的,所以我已经阅读了其他线程,但我仍然一无所获。我将我的项目设置为启动,我在 App.config 中有一个连接字符串。我在 VS2013 中使用 LocalDb。 如果我删除我的数据库,它会创建它
我在当前的项目中使用 EF Migrations 有一段时间了,一切都很好,直到今天,情况如下: 我做了一个小改动,添加了一个字符串属性 我调用了一个 API 方法并收到错误消息,指出模型发生了变化
这是我的两个模型类学生和类(class) public class Student { private readonly IStudentOps _studentOps; public
我的公司有一个实习生网站项目。我在 Code First 项目中使用 MVC5 和 Entity Framework 6。 这是我公司的内部网,所以我想要两个数据库。1 在生产中(发布)。1 在开发(
我使用 EF CodeFirst 并想使用来自包管理器控制台的命令行在数据库中重新创建表,该怎么做? 问题是在开发 EF CodeFirst 模型时经常更改,我手动删除数据库中的表,然后 EF 在第一
我想创建一个集成测试,它从数据库中获取一个 EF 实体,将其克隆到一个分离的对象,对其进行修改,然后将其保存回来并再次与原始对象进行比较。 但是,我使用 AutoMapper 创建类的克隆,但事实证明
我是一名优秀的程序员,十分优秀!