- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我看过其他一些答案和很多文章,但这个简单的部分仍然让我望而却步。我首先使用 EF 代码 4.1,但如果它更容易,我很乐意迁移到更新的版本。我有一个像这样的主要事实表:
namespace Spend.Models
{
public class ExpenseItem
{
[Key]
public String UniqueID_ERLineID { get; set; }
public String ERNum { get; set; }
public String ItemNum { get; set; }
public String Parent_Expense_Item { get; set; }
public String Card_Number { get; set; }
...
还有几个表卡在与 ExpenseItems 有多对一关系的表上:
public class ExpenseItemAccounting
{
[Key]
public String UniqueID_Accounting { get; set; }
public String ERLineID { get; set; }
public String ERNum { get; set; }
public String ItemNum { get; set; }
正如我们所见,第二个表中的 ERLineID 连接到第一个表中的 UniqueID_ErLineID,因此我通常依赖的“约定”不起作用。所以我需要使用虚拟 ICollection - 但我希望它将这些字段指定为链接。感谢任何有关如何执行此操作的帮助。
附言。我目前无法重命名数据库字段。
@卢克:
我应用了您提到的更改,它们很有意义。但是我收到以下错误:
System.Data.Entity.ModelConfiguration.ModelValidationException occurred
Message=One or more validation errors were detected during model generation:
System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'ExpenseItemAccounting_ExpenseItem_Target' in relationship 'ExpenseItemAccounting_ExpenseItem'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'ExpenseItemAccounting_ExpenseItem_Source' in relationship 'ExpenseItemAccounting_ExpenseItem'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be �1�.
Source=EntityFramework
StackTrace:
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.SelectMany[TSource,TCollection,TResult](IQueryable`1 source, Expression`1 collectionSelector, Expression`1 resultSelector)
at EmailClient.Prog.getData() in C:\MF\Dropbox\Dev_LN_Projects\04_QA\EmailClient\EmailClient\Prog.cs:line 172
InnerException:
这是在我尝试以下 linq 查询时出现的:
var geee = (from e in db.ExpenseItems
from f in db.ExpenseItemFbtItems
where
e.Item_Transaction_Date.Value.Year == 2011 &&
e.Item_Transaction_Date.Value.Month == 8
select new { A = e.UniqueID_ERLineID, B = f.ERLineID.First() });
我实际上希望能够说出 e.ExpenseItemAccounting.ItemNum 或类似的东西 - 我是否需要在 ExpenseItem 定义中添加一些东西才能实现这一点?
我的模型设置如下。 base.OnModelCreating 是通过 intellisense 出现的,我已经尝试过使用/不使用它以获得相同的结果:
public class SpendDB : DbContext
{
public DbSet<ExpenseAttachment> ExpenseAttachments {get; set; }
public DbSet<ExpenseComment> ExpenseComments {get; set; }
public DbSet<ExpenseItemAccounting> ExpenseAccountings {get; set; }
public DbSet<ExpenseItemFbtItem> ExpenseItemFbtItems {get; set; }
public DbSet<ExpenseItem> ExpenseItems {get; set; }
public DbSet<ExpenseItemViolation> ExpenseItemViolations {get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ExpenseItemAccounting>().HasOptional(e => e.ExpenseItem).WithMany().HasForeignKey(e => e.UniqueID_Accounting);
}
}
也许我需要将虚拟 ICollection 放入 ExpenseItem 定义中?或者它可能是另一种方式 - 说 modelBuilder.Entity 有可选的 ExpenseItemAccounting?这对我来说听起来更直观,但我(显然)不是很擅长这个,所以对它持保留态度!
再次感谢
最佳答案
这样做:
public class ExpenseItemAccounting
{
[Key]
public String UniqueID_Accounting { get; set; }
public ExpenseItem ExpenseItem{get;set;}
public String ERLineID { get; set; }
public String ERNum { get; set; }
public String ItemNum { get; set; }
}
然后在你的模型构建器中使用
modelBuilder.Entity<ExpenseItemAccounting>()
.HasOptional(e => e.ExpenseItem).WithMany()
.HasForeignKey(e => e.UniqueID_Accounting );
编辑:
要将导航属性配置为在另一端有一个集合,您可以像这样简单地添加它
public class ExpenseItem
{
[Key]
public String UniqueID_ERLineID { get; set; }
public String ERNum { get; set; }
public String ItemNum { get; set; }
public String Parent_Expense_Item { get; set; }
public String Card_Number { get; set; }
public ICollection<ExpenseItemAccounting> ExpenseItemAccountings{ get; set; }
}
然后通过如下修改模型构建器配置来连接它:
modelBuilder.Entity<ExpenseItemAccounting>()
.HasOptional(e => e.ExpenseItem).WithMany(e=> e.ExpenseItems)
.HasForeignKey(e => e.UniqueID_Accounting );
这会将它连接起来,以便 ExpenseItem 将具有所有子 ExpensItemAccounting 的列表,如果这样更有意义,您还可以添加它的单一版本,例如:
public class ExpenseItem
{
[Key]
public String UniqueID_ERLineID { get; set; }
public String ERNum { get; set; }
public String ItemNum { get; set; }
public String Card_Number { get; set; }
public ExpenseItemAccounting Parent_Expense_Item { get; set; }
}
并使用模型构建器进行配置:
modelBuilder.Entity<ExpenseItemAccounting>()
.HasOptional(e => e.ExpenseItem)
.WithOptionalDependent(e=>e.Parent_Expense_Item);
我想如果您还想连接 FK 引用(不仅仅是导航属性),您需要在单独的语句中执行此操作,但这有点麻烦。
查看有关导航属性的 MSDN 页面以及如何使用 modelBuilder,因为它有很多很好的高级示例。
http://msdn.microsoft.com/en-us/library/hh295843(v=vs.103).aspx
关于c# - EF 代码优先 : Defining foreign keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612611/
我正在尝试添加 ace editor到我的应用程序。我从 github 下载它,将“ace/lib/ace”目录放到我的应用程序目录中,包括: " 在我的正文标签中: editor = ace.edi
如果有人能介绍一下 TypeScript 1.8 模块,我将不胜感激。 我有 SomeClass.ts 文件: export class SomeClass { } 然后,在我的 app.ts 中导入
今天,当我阅读php内核代码时,在目录:php5.3/Zend/Zend.h中,有一行代码让我很困惑。 代码如下: /* overloaded elements data types */ #defi
今天看php的内核代码,在php5.3/Zend/Zend.h目录下,有一行代码搞得我一头雾水。 代码如下: /* overloaded elements data types */ #define
我有一个相当大的项目,我试图将所有 JS 文件连接到一个文件中。我已经能够做到这一点,但我在实际将这两个文件实现到代码中时遇到了麻烦。 我的 Gruntfile 有一个执行串联的任务: require
1在以下示例中似乎没有必要(并且可能具有误导性),但是在用于检查 #ifdef 时我已经多次看到这种情况s: #ifndef __NEWLIB_H__ #define __NEWLIB_H__ 1 使
感谢您提供的任何帮助。我有一个 Grails 项目,我正在尝试将 Karma 安装到其中。但是,当我运行时 ./gradlew 构建 我收到以下错误: [x-10-105-56-234]SENG519
我从 Frontend Developer Interview Coding Questions 得到了这个问题.为什么下面的代码片段会打印它打印的内容?我最初认为它会抛出一个 ReferenceEr
之前的问题描述有歧义,所以我在下面修改了一些东西。谢谢。 我想像这样实现一些宏: #define AddVariable(x) \ #define x (++counter) class Ba
我知道我正试图朝自己的腿开枪 ;) 但是,它可以让我使其余(大量)代码更小且更易读。 有什么技巧可以在另一个预处理器宏中创建预处理器宏吗? 这是我正在寻找的示例。我的真实场景更复杂 // That's
在 Microsoft 的 WinDef.h 中引入了几个用于回调的#defines: #ifdef _MAC #define CALLBACK PASCAL #define WINAPI
我知道我想在腿上开枪;)但是,它可以让我使其余(大量)代码更小且更具可读性。 在另一个预处理器宏中创建预处理器宏有什么棘手的方法吗? 这是我正在寻找的示例。我的真实场景更复杂 // That's wh
当我使用这段代码时: #include #define STR(x) #x int main(void) { printf(__FILE__ STR(__LINE__) "hello!\n"
今天的问候, 您好,我是使用 vb 6.0 的初学者。我正在使用以下代码并获得“用户定义类型未定义”。代码在下面。我得到错误的行被突出显示。请帮助。我应该添加一些引用或组件吗?如果是这样,它会是什么。
当我尝试编译此代码时,在 VB6 中出现编译错误“用户定义的类型未定义”。谁能告诉我需要做什么来解决这个错误?以下是我收到错误的行: Public Conn As ADODB.Connection P
我最近将我的应用程序从 Ember 1.3 升级到了 Ember 2.4.2。然而,在使用 ember build --env production 将其部署到我的生产环境后,我注意到了一些问题。 第
这个问题在这里已经有了答案: Getting user-defined type not defined error when running code (2 个回答) 3年前关闭。 我在运行程序以通
在我的一个项目中,我最近进行了重构并重命名了很多。完成此步骤后,一切正常,所有单元测试都通过了,但是我遇到了以下问题。 当我在VBE中执行“编译”时,确实出现了“用户定义的类型未定义”错误,并出现两种
所以我的 Visual Studio 将 tag1 和 tag2 都声明为未定义,但它们的定义很清楚,我不能根据另一个定义一个吗? #define push 99 #de
在 Excel 2007 中,我有以下非常简单的 VBA 代码: Public Type specType sb As Long End Type Private Sub MyButton_C
我是一名优秀的程序员,十分优秀!