简体   繁体   English

您如何处理SubSonic与迁移的“关系”?

[英]How do you handle SubSonic 'relationships' with migration?

According to this article: http://subsonicproject.com/docs/3.0_Migrations 根据这篇文章: 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. Disaster包含一个由灾难造成的多个AddressesIList 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. 当我使用SimpleRepository把这些东西加到与数据库SimpleRepositoryOptions.RunMigrations ,它产生的所有列的表,但没有外键列如预期。

How would I relate these two together so that when I call Disaster.Addresses , I get a list of all the affected Addresses ? 我如何将这两个地址关联在一起,以便在我调用Disaster.Addresses ,得到所有受影响Addresses的列表? Is this possible or do I have to use ActiveRecord instead and create the database tables first? 这是否可能,或者我必须改用ActiveRecord并首先创建数据库表吗? Or do I have to add in a column for the disaster's ID into Address ? 还是我在为灾区的ID到添加一列Address If so, how does this method work for many-to-many relationships? 如果是这样,这种方法如何用于多对多关系?

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). 在“灾难”中添加一个名为“ Addresses”的属性,并将其设置为IList <>(如果希望将其延迟加载,则可以将其设置为IQueryable)。 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? 底线-SubSonic如何知道这一点? 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. 我们可以内省两个对象的“双向性”,这意味着如果Address有很多灾难而不是很多到很多(这很明显),但是如果您喜欢DDD,那么编码就不高兴了。

I'm leaning towards that rule with some type of override that would force the issue. 我倾向于使用某种强制类型的替代规则。 Your thoughts on this are welcome :) 欢迎您对此发表看法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM