有谁知道如何删除ADO.NET实体框架中的多对多关系,而无需加载所有数据?在我的情况下,我有一个实体主题有一个属性订阅,我需要删除一个订阅.代码myTopic.Subscriptions.Remove(...)有效,但我需要先加载所有订阅(例如myTopic.Subscriptions.Load()),我不想这样做,因为有很多(我的意思是很多)订阅
所以,我正在使用Linq实体框架.我有2个实体:Content和Tag.他们彼此处于多对多的关系中.Content可以有很多Tags,Tag可以有很多Contents.所以我试图写一个查询来选择任何标签名称相等的所有内容blah
实体都将另一个实体的集合作为属性(但没有ID).这是我在努力的地方.我有一个自定义表达式Contains(所以,无论谁可以帮助我,你可以假设我可以为一个集合做一个"包含").我得到了这个表达式:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID = 2670710&SiteID = 1
如何使表user_roles将两列(userID,roleID)定义为复合主键.应该很容易,只是记不住/找不到.
在user实体中:
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles")
public List<RoleDAO> getRoles() {
return roles;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getUserID() {
return userID;
}
Run Code Online (Sandbox Code Playgroud)
在roles实体中:
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles")
public List<UserDAO> getUsers() {
return users;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getRoleID() {
return roleID;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
**更多信息
因此,有一个第三表user_roles即花费(由上述自动生成)userID从user实体和roleID从roles实体.现在我需要生成的table(user_roles)中的这两列是一个复合主键.
我在rails中有很多关系.所有数据库表都相应地进行相应命名.所有模型文件都是复数,并使用下划线分隔单词.所有命名通知都遵循ruby和rails标准.我在我的模型中使用了很多这样的:
has_many :users, :through => :users_posts #Post model
has_many :posts, :through => :users_posts #User model
belongs_to :users #UsersSource model
belongs_to :posts #UsersSource model
Run Code Online (Sandbox Code Playgroud)
这个错误还有什么呢?
ActiveRecord::HasManyThroughAssociationNotFoundError in UsersController#welcome
Could not find the association :users_posts in model Post
activerecord many-to-many ruby-on-rails associations has-many-through
如何使用Django使用复合(复合)主/唯一键创建模型(以及表格)?
我在模特中遇到了一场冲突:
class Visit(models.Model):
user = models.ForeignKey(User)
visitor = models.ForeignKey(User)
Error: One or more models did not validate:
profiles.visit: Accessor for field 'user' clashes with related field 'User.visit_set'. Add a related_name argument to the definition for 'user'.
profiles.visit: Accessor for field 'visitor' clashes with related field 'User.visit_set'. Add a related_name argument to the definition for 'visitor'.
Run Code Online (Sandbox Code Playgroud)
在访客领域使用什么是明智的'related_field'?此模型基本上表示对特定用户的个人资料发生的访问.
我还应该用ManyToManyField替换任何ForeignKey吗?逻辑有点令人困惑.
编辑:这似乎解决了它,但我不确定这是否是我想要的.:)
class Visit(models.Model):
user = models.ForeignKey(User)
visitor = models.ForeignKey(User, related_name='visitors')
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用foos和bars 建立一个SQLite3数据库,并在它们之间建立多对多关系.这是我到目前为止所得到的:
CREATE TABLE foo(
id INTEGER PRIMARY KEY NOT NULL,
foo_col INTEGER NOT NULL
);
CREATE TABLE bar(
id INTEGER PRIMARY KEY NOT NULL,
bar_col TEXT NOT NULL
);
CREATE TABLE foobar(
foo_id INTEGER,
bar_id INTEGER,
FOREIGN KEY(foo_id) REFERENCES foo(id) ON DELETE CASCADE,
FOREIGN KEY(bar_id) REFERENCES bar(id) ON DELETE CASCADE
);
CREATE INDEX fooindex ON foobar(foo_id);
CREATE INDEX tagindex ON foobar(tag_id);
Run Code Online (Sandbox Code Playgroud)
......但它似乎没有起作用.我可以删除一行foo,但不会影响foobar.我究竟做错了什么?
在我的Symfony2项目中,我有两个相关的实体:用户和收藏夹.他们有多对多的关系.
我的应用程序的工作原理如下:在我的Twig页面中,我有一些带有"添加到收藏夹"按钮的项目.当您单击按钮时,我的控制器将item_id保存在"收藏夹"列中.但后来我想保存将项目添加到他的收藏夹的用户,这里我的应用程序失败了.
用户和收藏夹存在但用户和收藏夹之间的连接列仍为空.我也没有收到任何错误.
这是我的代码:
实体用户
class Users implements AdvancedUserInterface
{
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Favorites", inversedBy="user", cascade={"persist"})
* @ORM\JoinTable(name="user_has_favorite",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="favorite_id", referencedColumnName="favorite_id")
* }
* )
*/
private $favorite;
public function __construct()
{
$this->favorite = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addFavorite(\Geo\CityTroopersBundle\Entity\Favorites $favorite)
{
$this->favorite[] = $favorite;
return $this;
}
...
Run Code Online (Sandbox Code Playgroud)
实体收藏夹
class Favorites
{
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Users", mappedBy="favorite", cascade={"persist"})
*/
private $user;
public function __construct() …Run Code Online (Sandbox Code Playgroud) 我有两个型号如下:
class Book(models.Model):
title = models.CharField(max_length=100)
year = models.IntegerField(max_lenght=4)
author = models.ManyToManyField(null=true, blank=true)
class Author(models.CustomUser):
# some fields
Run Code Online (Sandbox Code Playgroud)
现在,我要做的是添加一个Author多个Book对象而不迭代书对象列表.
Django的更新方法不支持ManyToManyField按文档.它说
您只能使用此方法设置非关系字段和ForeignKey字段.要更新非关系字段,请将新值作为常量提供.要更新ForeignKey字段,请将新值设置为要指向的新模型实例.
所以目前我正在做以下事情,这是非常低效的,因为我将为每个图书对象访问数据库.
author = Author.objects.get(pk=1)
books = Book.objects.filter(**kwargs) # say this returns 100 objects
# each loop here will hit the database making it really inefficient
# for long lists.
for book in books:
book.authors.add(author)
book.save()
Run Code Online (Sandbox Code Playgroud)
我很确定有一种解决方法,但我无法在文档中找到它.任何帮助,将不胜感激.谢谢
默认情况下,ManyToManyDoctrine 下的自引用关系涉及拥有方和反方,如文档中所述.
有没有办法实现双方之间没有差异的互惠协会?
按照文档中的示例:
<?php
/** @Entity **/
class User
{
// ...
/**
* @ManyToMany(targetEntity="User")
**/
private $friends;
public function __construct() {
$this->friends = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
因此,加入entity1到entity2小号friends 意味着entity2会在entity1朋友.
many-to-many ×10
database ×3
django ×3
activerecord ×1
ado.net ×1
annotations ×1
associations ×1
bulk ×1
c# ×1
compound-key ×1
doctrine ×1
doctrine-orm ×1
java ×1
lambda ×1
linq ×1
orm ×1
persistence ×1
php ×1
primary-key ×1
save ×1
sqlite ×1
symfony ×1