你如何处理 SubSonic 的“关系”?与迁移?
根据这篇文章: http://subsonicproject.com/docs/3.0_Migrations
Bottom line: if you're a developer that is concerned about database design,
migrations might not be for you.
好的,没关系,我可以治疗数据库只是一个不包含任何业务逻辑的持久数据存储库。换句话说,一个美化的文本文件。
我不知道如何将两个对象关联在一起。以这两个类为例:
public class Disaster
{
public int DisasterId { get; set; }
public string Name { get; set; }
public DateTime? Date { get; set; }
public IList<Address> Addresses { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string WholeAddressHereForSakeOfBrevity { get; set; }
}
Disaster
包含一个包含多个遭受灾难的Addresses
的IList
。当我使用 SimpleRepositoryOptions.RunMigrations 将这些添加到数据库时,它会生成包含所有列的表,但没有预期的外键列。
如何将这两者联系起来,以便在调用 Disaster.Addresses
时,获得所有受影响的 Addresses
的列表?这是可能的还是我必须使用 ActiveRecord 并首先创建数据库表?或者我是否必须在 Address
中添加灾难 ID 列?如果是这样,这种方法如何适用于多对多关系?
According to this article:
http://subsonicproject.com/docs/3.0_Migrations
Bottom line: if you're a developer that is concerned about database design,
migrations might not be for you.
Ok, that's fine, I can treat the database as simply a persistent repository of data that doesn't contain any business logic. In other words, a glorified text file.
What I don't know how to do is relate two objects together. Take for example these two classes:
public class Disaster
{
public int DisasterId { get; set; }
public string Name { get; set; }
public DateTime? Date { get; set; }
public IList<Address> Addresses { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string WholeAddressHereForSakeOfBrevity { get; set; }
}
Disaster
contains an IList
of multiple Addresses
that were hit by the disaster. When I use SimpleRepository
to add these to the database with SimpleRepositoryOptions.RunMigrations
, it generates the tables with all the columns, but no foreign key columns as expected.
How would I relate these two together so that when I call Disaster.Addresses
, I get a list of all the affected Addresses
? Is this possible or do I have to use ActiveRecord instead and create the database tables first? Or do I have to add in a column for the disaster's ID into Address
? If so, how does this method work for many-to-many relationships?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的——您只需手工完成即可。向 Disaster 添加一个名为“Addresses”的属性,并将其设为 IList<> (或者如果您希望延迟加载,您可以将其设为 IQueryable)。当您检索灾难时,请务必检索您的地址。
这有点像“手册”——但这就是想法。我正在努力对此进行增强,希望在以后的版本中推出。
在你问为什么我一开始没有这样做之前:)这是因为我不知道我是否应该根据父/子关系使用多对多或一对多。在您的示例中,我猜测它可能是一对多,但根据我对地址和灾难的了解(尤其是在佛罗里达州),它可能应该是多对多。
底线 - SubSonic 如何知道这一点?我们可以内省这两个对象的“双向性”,这意味着如果 Address 有很多灾难,那么它是多对多的(这是显而易见的)——但是如果您喜欢 DDD,那么这并不是愉快的编码。
我倾向于通过某种类型的覆盖来强制解决该问题。欢迎您对此的想法:)
It's possible - you just do it by hand is all. Add a property to Disaster called "Addresses" and make it an IList<> (or you can make it IQueryable if you want it to Lazy Load). When you retrieve your Disaster, just be sure to retrieve your Addresses.
It's sort of "manual" - but that's the idea. I'm working on enhancements to this that i'm hoping to push in a later release.
And before you ask why I didn't do it in the first place :) it's because I don't know if I should use a Many to Many or 1-many based on the parent/child relationship. In your example, I'd guess that it's probably 1 to many but given what I know about Addresses and disasters (especially in Florida) it should probably be many to many.
Bottom Line - how would SubSonic know this? We could introspect both objects for "bi-directionality", which means if Address has many disasters than it's many to many (which is obvious) - but then that's not happy coding if you like DDD.
I'm leaning towards that rule with some type of override that would force the issue. Your thoughts on this are welcome :)