- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下使用 Entity Framework Code First 方法的 C# 代码。表是在数据库中创建的;但输入的数据不正确。
Person 1 is member of Club 1 and Club 3.
Person 2 is member of Club 2 and Club 3
That means Club 2 has only one member.
但是使用下面的查询可以看出,到达数据库的数据是不正确的。
需要对 C# 代码进行哪些更改才能使其正确?
static void Main(string[] args)
{
Database.SetInitializer<NerdDinners>(new MyInitializer());
string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{
Club club1 = new Club();
Club club2 = new Club();
Club club3 = new Club();
Person p1 = new Person();
Person p2 = new Person();
List<Club> clubsForPerson1 = new List<Club>();
clubsForPerson1.Add(club1);
clubsForPerson1.Add(club3);
List<Club> clubsForPerson2 = new List<Club>();
clubsForPerson2.Add(club2);
clubsForPerson2.Add(club3);
List<Person> personInClub1 = new List<Person>();
personInClub1.Add(p1);
List<Person> personInClub2 = new List<Person>();
personInClub2.Add(p2);
List<Person> personInClub3 = new List<Person>();
personInClub3.Add(p1);
personInClub3.Add(p2);
club1.Members=personInClub1;
club2.Members=personInClub2;
club3.Members=personInClub3;
p1.Clubs = clubsForPerson1;
p2.Clubs = clubsForPerson2;
db.Clubs.Add(club1);
db.Clubs.Add(club2);
db.Clubs.Add(club3);
db.Persons.Add(p1);
db.Persons.Add(p2);
int recordsAffected = db.SaveChanges();
}
}
namespace LijosEF
{
public class Person
{
public int PersonId { get; set; }
public virtual ICollection<Club> Clubs { get; set; }
}
public class Club
{
public int ClubId { get; set; }
public virtual ICollection<Person> Members { get; set; }
}
public abstract class PaymentComponent
{
public int PaymentComponentID { get; set; }
public int MyValue { get; set; }
public abstract int GetEffectiveValue();
}
public partial class GiftCouponPayment : PaymentComponent
{
public override int GetEffectiveValue()
{
if (MyValue < 2000)
{
return 0;
}
return MyValue;
}
}
public partial class ClubCardPayment : PaymentComponent
{
public override int GetEffectiveValue()
{
return MyValue;
}
}
public partial class Payment
{
public int PaymentID { get; set; }
public List<PaymentComponent> PaymentComponents { get; set; }
public DateTime PayedTime { get; set; }
}
public class MyInitializer : CreateDatabaseIfNotExists<NerdDinners>
{
//Only one identity column can be created per table.
protected override void Seed(NerdDinners context)
{
}
}
//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{
public NerdDinners(string connString): base(connString)
{
}
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
//Fluent API - Plural Removal
modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public DbSet<Person> Persons { get; set; }
public DbSet<Club> Clubs { get; set; }
}
}
最佳答案
其实没有问题。由于 id 创建的顺序,我认为这是一个问题。我在 Entity Framework: Duplicate Records in Many-to-Many relationship 中发布了一些其他问题.
它使用
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)entity);
关于c# - Entity Framework : Incorrect data while using Many-to-many relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643741/
我有模特儿 class Category(models.Model): title = models.CharField(...) entry
我确实有一个“引用”表和一个“文章”表,其中一篇文章引用了其他文章。 我确实有一些简单的引用资料,例如:A -> B SQL: select ab.* from Article a inner joi
我目前正在研究发布有界上下文。这方面的主要参与者是产品 和 上市 . 产品:可以在多个市场上列出。一 产品 许多 上市 . 列表:可以有很多产品,因为某些市场支持变体列表。一 上市 许多 产品 . 基
我有一个简单的化妆品数据库。每个产品可能包含多种成分。因此它是一个简单的多对多关系,目前我有一个这样的数据库: CREATE TABLE `products` ( `id` INT UNSIGNE
我只是在学习 django 并遵循教程。我有一个链接和一个书签。与我正在关注的教程不同,我希望一个链接只与一个书签相关联,但一个书签可以有多个链接。这是设置模型的方法吗? class Link(mod
我有 3 个表;用户、组和权限 在模型中,我将关系设置为 belongsToMany在用户模型中: public function groups() { return $this->belon
与 Laravel 4 中的一个问题作斗争,在“联系”模型编辑表单中,我可以获得所有字段的当前值,除了与另一个模型“公司”建立关系的多重选择中的那些。这是一个多对多的关系。我正在获取公司列表,但即使存
我正在制作现有数据库的模型。我的表通过连接表具有多对多的自引用。连接表有两个字段:“parent_id”和“child_id”。 我正在尝试这样定义关联: company.hasMany(co
据我所知 查找表当我们处理 时是必要的多对多 关系。 但是呢?一对多关系 - 我们需要 查找表或 外键 在其中一张 table 上? 另一个问题,是一对多同 多对一 ? 最佳答案 一对多关系不需要查找
当我启动服务器时,在建立数据库连接后不久,我会这样做: var tool = require("./tool"); //created with Sequelize.define() var requ
对不起,如果标题没有多大意义,如果您对我的问题的更准确描述有建议,请提出建议。 我正在开发一个 Symfony 2 应用程序,我有必须与另一个用户相关的用户对象,简单的用例是 friend ,但也有更
这可能只是因为我缺乏对 EF Code First fluent API 的了解,但我很困惑。 我想模拟以下内容: 一个包含 Id 和 Name 的Groups集合 一个包含 Id 和 Name 的U
在关系数据库中,我有一个用户表、一个类别表和一个执行多对多关系的用户类别表。 这个结构在Redis中是什么形式的? 最佳答案 对于 Redis,关系通常由集合表示。一套可以用表示单向关系,因此每个对象
我正在为我的数据库使用 MySQL,并且在我的 Django 应用程序中有以下 Message 模型: class Message(models.Model): sender = models
我有这种情况,但我不确定应该如何在数据库中对其进行建模。我试图建模的对象是:球队、球员、球队球员的成员资格,以及给定球队中每个球员的应付费用列表。因此,费用取决于球队和球员。 因此,我目前的做法如下:
我有一个实体 Book,它与其他实体的关系太多。现在我想对多对多关系属性之一的 COUNT 列进行排序,我使用这种方法 Doctrine2 order by count many to many Do
必须改变实体之间的关系,我想知道改变关联映射类型是否正常,数据库中已经存在的数据是否会正常传输?我试图找到有关它的信息,但没有找到。或者如果映射将被更改,则必须通过 sql 查询手动传输已经存在的数据
我有一个丑陋的循环,它在 Django 中作为创可贴运行,我想将其优化为连接。我已将其分解为最基本的结构,以帮助简化问题。我希望其他人以前遇到过类似的问题 - 如果没有,我会在最终修复它时分享答案。
我想使用 Spring Boot 和 Spring Data REST 构建一个简单的 HATEOAS API。 因此,我创建了2个不同的实体A和B。两者之间存在多对多关系。由于该关系还需要 2 个属
我有两个名为“类别”和“文章”的实体,它们具有多对多关系。我想形成一个谓词来搜索 category.name 等于某个值的所有文章。我有以下内容: NSEntityDescription *ent
我是一名优秀的程序员,十分优秀!