我有一个WCF可以调用我的EF。我已经将WCF作为启动项目,其中一个服务是启动文件。
当我运行这个项目并传递正确的参数时,就像我在Seed方法中所做的那样,我从登录方法返回一个对象。
这意味着应该在SQL Server的本地服务器上创建数据库吗?当我更改代码的第一个类时,我得到一个错误,指出模型已经改变,所以这是因为数据库已经存在了吗?
我可以在Visual Studio Server资源管理器中看到数据库,但它已断开连接,当我尝试点击它时,如果我没有将其配置为在我的 Web.Config中有一个用户名和密码,则需要输入用户名和密码
可能是我的问题。我想在SQL服务器中看到我的EF数据库。
用户背景
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
public UserContext()
: base("Local")
{
Configuration.AutoDetectChangesEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
}
用户
public class User
{
[Key]
public Guid Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public ICollection<UserTask> Tasks { get; set; }
public User()
{
Tasks = new List<UserTask>();
}
}
用户服务(WCF)
public class UserService : IUserService
{
public User Login(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
throw new Exception("User name or password cannot be empty");
}
User user = null;
using (var userContext = new UserContext())
{
user =
userContext.Users.FirstOrDefault(
u => u.Username.Equals(username, StringComparison.InvariantCulture) &&
u.Password.Equals(password, StringComparison.InvariantCulture));
}
return user;
}
}
的Web.config
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<!-- local dev -->
<add name="Local" connectionString="server=.;database=TodoTest;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
答案 0 :(得分:0)
尝试以管理员身份运行VS. 另外,公共ICollection Tasks {get;组;应该是虚拟的。
关于您的问题“我收到一条错误,指出模型已更改,因为这是因为数据库已存在?”答案是肯定的......:)