实体框架代码优先和身份

时间:2017-03-31 07:26:12

标签: asp.net-mvc entity-framework code-first

我有一个三层项目(DAL,Domain,MVC)。我需要使用AuthorEntity来绑定ApplicationUser,这是MVC项目中的context(EfContext, ApplicationDbContext)

他们有不同的UserEntity

首先,我创建了IUser并尝试实施string field Id (sting Id{get;}),其中包含[Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] ,我收到了错误消息:

  

“EntityType'UserEntity'没有定义键。为此定义键   的EntityType“

我添加了属性:

public void SaveConnectionString(DbInfo dbinfo, string path,string appConfigFile)
    {
        try
        {
            string configFile = Path.Combine(path, appConfigFile);
            var doc = new XmlDocument();
            doc.Load(configFile);
            XmlNodeList endpoints = doc.GetElementsByTagName("connectionStrings");
            foreach (XmlNode item in endpoints)
            {
                if (item.HasChildNodes)
                {
                    foreach (var c in item.ChildNodes)
                    {
                        if (((XmlNode)c).NodeType == XmlNodeType.Element)
                        {


                            var adressAttribute = ((XmlNode)c).Attributes["name"];
                            if (adressAttribute.Value.Contains("YourConStrName"))
                            {
                                if (dbinfo.dbType == dataBaseType.Embedded)
                                {
                                    ((XmlNode)c).Attributes["connectionString"].Value = SetupConstants.DbEmbededConnectionString;
                                    ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbEmbededConnectionProvider;
                                }
                                else if (dbinfo.dbType == dataBaseType.Microsoft_SQL_Server)
                                {
                                    if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.SQL_Server_Authentication)
                                    {
                                       // ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, SetupConstants.SqlDbName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";
                                        ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, dbinfo.DatabaseName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";

                                    }
                                    else if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.Windows_Authentication)
                                    {
                                        //((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, SetupConstants.SqlDbName);
                                        ((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, dbinfo.DatabaseName);
                                    }
                                    ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbSqlConnectionProvider;
                                }
                            }
                        }
                    }
                }

            }
            doc.Save(configFile);
            string exePath = Path.Combine(path, "EDC.Service.exe");
            InstallerHelper.EncryptConnectionString(true, exePath);
        }
        catch (Exception ex)
        {
            //TODO://log here exception
            Helper.WriteLog(ex.Message + "\n" + ex.StackTrace);
            throw;
        }
    }

它不起作用。怎么做?

1 个答案:

答案 0 :(得分:0)

主键属性必须是可读写的。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id{get;set;}