Linq to sql 使用Moq模拟Linq到Sql EntityRef?

Linq to sql 使用Moq模拟Linq到Sql EntityRef?,linq-to-sql,moq,entityreference,Linq To Sql,Moq,Entityreference,My datacontext包含一个名为Userlog的表,该表与表UserProfile具有一对多关系 类内用户日志 public UserProfile UserProfile { get {return this._UserProfile.Entity;} } 类内用户配置文件 public EntitySet<UserLog> UserLogs { get{return this._UserLogs;} } { set {this._UserLogs.Assig

My datacontext包含一个名为Userlog的表,该表与表UserProfile具有一对多关系

类内用户日志

public UserProfile UserProfile
{
  get {return this._UserProfile.Entity;}
}
类内用户配置文件

public EntitySet<UserLog> UserLogs
{
  get{return this._UserLogs;}
}
{
  set {this._UserLogs.Assign(value);
}
public EntitySet用户日志
{
获取{返回此。\u UserLogs;}
}
{
设置{this.\u UserLogs.Assign(值);
}
在不更改自动生成的代码的情况下,如何进行模拟(使用Moq)UserLog.UserProfile

我想做的是:

var mockUserLog = new Mock<UserLog>();
mockUserLog.Setup(c=>c.UserProfile.FirstName).Returns("Fred");
var mockUserLog=new Mock();
mockUserLog.Setup(c=>c.UserProfile.FirstName)。返回(“Fred”);

如果我进入designer.cs并将FirstName和UserProfile设置为虚拟,我就可以这样做,但是我希望在分部类中这样做

有什么想法吗

谢谢
Jeremy

之所以需要将该方法设置为虚拟方法,是因为您正在模拟一个直接类。实现这一点的最佳方法是创建一个分部类(如您所建议的)并让该分部类包含一个接口。因此,您将有如下内容:
designer.cs->公共部分类用户日志
UserLog.cs->公共部分类UserLog:IUserLog
IUserLog.cs->应该实现的方法(方法将由designer.cs实现)

您创建的分部类(UserLog.cs)仅用于实现IUserLog。然后,IUserLog将包含所有需要实现的方法,如:public UserProfile UserProfile()

完成后,可以模拟接口而不是类。

我希望这会有所帮助,但可能比我解释的要困难。

您需要将方法设置为虚拟的原因是因为您正在模拟一个直接类。实现这一点的最佳方法是创建一个分部类(如您所建议的)并让该分部类包含一个接口。因此,您将有如下内容:
designer.cs->公共部分类用户日志
UserLog.cs->公共部分类UserLog:IUserLog
IUserLog.cs->应该实现的方法(方法将由designer.cs实现)

您创建的分部类(UserLog.cs)仅用于实现IUserLog。然后,IUserLog将包含所有需要实现的方法,如:public UserProfile UserProfile()

完成后,可以模拟接口而不是类。
我希望这有帮助,但可能比我解释的要困难