开发者

MVC2 - Are There Signals for Models?

I'm just starting on my first ASP.NET MVC project (it's actually extending an existing MVC project that uses 4.0 and Linq2SQL). What I'm looking for is a way to create an instance of a model every time a different model is created (i.e., saved to the database). I see an OnCreated() method in the generated code, but that's in a partial class, so I can't replace it/ override it.

Is there a way to tie things together like this? I feel like I've been working in Django so long (where I would use a signal) that I don't know the right term to search for.

EDIT: What I want to do -- every time开发者_运维知识库 an instance of Model A is saved to the database for the first time (and only the first time), I want to create an instance of Model B and save that to the database.

And it looks like OnCreated() doesn't have anything to do with that.


You can always override the save method.

class A(models.Model)

    def save(self,**kwargs):
        super(A,self).save(**kwargs)
        if self.pk:   #Primary Key is assigned only after the save.
            B.objects.create()
        return self
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜