0

我有一个调用我的 EF 的 WCF。我已将 WCF 设为我的启动项目,并将其中的一项服务设为启动文件。

当我运行这个项目并传入正确的参数作为我在我的 Seed 方法中的参数时,我从Login方法中获取了一个对象。

这意味着数据库应该是在我的本地服务器上创建的 SQL Server 对吗?当我更改代码第一类时,我收到一个错误,指出模型已更改,所以这是因为数据库已经存在对吗?

我可以在 Visual Studio Server 资源管理器中看到数据库,但它已断开连接,当我尝试单击它时,当我尚未将其配置为在我的Web.Config中有用户名和密码时,它会询问用户名和密码

什么可能是我的问题。我想在 SQL Server 中查看我的 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;
    }
}

网页配置

<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>
4

1 回答 1

0

尝试以管理员身份运行 VS。
此外,公共 ICollection 任务 { get; 放; } 应该是虚拟的。

关于您的问题“我收到一个错误,指出模型已更改,所以这是因为数据库已经存在?” 答案是肯定的...... :)

于 2015-06-05T08:44:48.393 回答