我按照此答案中的详细说明进行操作,我的枚举已保存并可在整个应用程序中使用。但如果我想添加其他选项或更改现有选项的名称,我该怎么做?
我知道我可以通过在文本编辑器中手动编辑 EDMX 文件来实现。如果可能的话,我宁愿在 Visual Studio 中以类似于我生成枚举的方式开始。
我有一些 EF Core 模型,它们是使用dotnet ef dbContext scaffold数据库优先方法构建的,用于生成模型。我的问题是数据库使用整数主键,用于将表链接在一起,但有一个基于字符串的索引,可用作搜索表的合理索引。
但是:当我尝试使用时,FindAsync("abc000")我得到了一个完全预期的错误The key value at position 0 of the call to 'DbSet<Entity>.Find' was of type 'string', which does not match the property type of 'long'.
所以,两个问题:
它们看起来像这样:
class Entity
{
long Id;
string Key;
};
Run Code Online (Sandbox Code Playgroud)
在 OnModelCreating 中:
modelBuilder.Entity<Entity>(entity =>
{
entity.ToTable("tb_entity", "main");
entity.HasIndex(e => e.Key)
.HasName("uq_entity_key")
.IsUnique();
entity.Property(e => e.Id).HasColumnName("_id");
entity.Property(e => e.Key)
.HasColumnName("key")
.HasMaxLength(255);
}
Run Code Online (Sandbox Code Playgroud)
创建表的 SQL 如下所示:
CREATE TABLE [tb_entity]
(
_id …Run Code Online (Sandbox Code Playgroud) 想象一下这个数据库模型:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Firstname { get; set; }
public ICollection<Role> Roles { get; set; }
}
public class Role
{
public int Id { get; set; }
public string RoleType { get; set; }
public ICollection<User> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有一个看起来像这样的中间表(不作为POCO出现):
UserRole UserId RoleId
然后我决定删除一个角色,这意味着也应删除该角色在中间表中的所有关系.
无论我尝试什么,我都会收到以下错误消息:
DELETE语句与REFERENCE约束"FK_UserRole_Role"冲突.冲突发生在数据库"dbname",表"dbo.UserRole",列"RoleId"中.
或者此错误消息:
无法删除该对象,因为在ObjectStateManager中找不到该对象.
第一条错误消息来自此尝试:
_dataContext.Entry(role).State = EntityState.Deleted;
_dataContext.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
这个负责第二条错误消息:
_dataContext.Circuit.Remove(role);
_dataContext.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
我做了一些其他的尝试,但我不记得了,因为我一直试图从今天早上开始工作(GMT +2).
有人能指出我正确的方向吗?
我想连接名字和姓氏并在视图中将其显示为全名。我在 usermeta 中尝试过,因为当您再次生成 edmx 文件时,它不会影响但错误无法识别的字段,知道如何吗?
public partial class userdetail
{
public userdetail()
{
this.orderdetails = new HashSet<orderdetail>();
}
public string userid { get; set; }
public string username { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public virtual ICollection<orderdetail> orderdetails { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我又创建了一个用于验证的类 usermeta。
public class Usemeta
{
[Required]
public string userid { get; set; }
[Required]
public string username { get; set; }
[Required]
public string …Run Code Online (Sandbox Code Playgroud) 最近我使用VS2017创建了一个ASP.NET Core(多平台)项目和一个用于管理SQL Server数据库模型的ClassLibrary.
实际上我们在生产服务器中有一个数据库,我需要使用dotnet ef dbcontext scaffold [arguments] [options] ...命令行生成类,但是我需要在创建DbContext类时单一化类名.
请,需要帮助!谢谢
我首先使用 EF 6 数据库。我使用这种方法来获取表的名称:
private static string GetTableName(ObjectContext context, Type entityType)
{
string entityTypeName = entityType.Name;
EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
string tableName = (from meta in container.BaseEntitySets
where meta.ElementType.Name == entityTypeName
select meta.Name).First();
return tableName;
}
Run Code Online (Sandbox Code Playgroud)
但它只返回表名,我在数据库中有 5 个模式,每个模式对应一个单独的DbContext,因此它可能具有不同模式的相同名称。所以我需要返回表的全名
基本上我已经创建了一个MVC3应用程序,它连接并更新服务器上存在的数据库.然而,这个数据库是测试数据库,当我实际推出项目时,我需要它连接到实际的数据库.我的问题是将项目/应用程序更改为指向这个新数据库有多难?
我使用数据库第一种方法,并且我知道我需要更改我的连接字符串.我觉得这不可能那么简单.
任何帮助,技巧或教程将不胜感激.
c# sql-server asp.net-mvc-3 ef-database-first visual-studio-2012
我对数据库优先模型以及如何使用 MVC 视图模型处理这些模型几乎没有经验。
很明显,我无法更改原始模型,但我想为模型添加一些注释以进行验证。
因此,我试图做的是创建一个从实体继承的 ViewModel
我的 DB First 生成的模型 - 不要修改
public partial class xmldata
{
public string ISBN { get; set; }
public string title { get; set; }
public string blurb { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个视图模型,如下所示,它继承自 xmldata
public class XmlDataViewModel : xmldata
{
[AllowHtml]
[Display(Name = "Blurb")]
public string BlurbVm {
get { return blurb; }
set { blurb = value; }
}
...
}
Run Code Online (Sandbox Code Playgroud)
上面显示的字段我需要 AllowHtml 和我迄今为止最好的解决方案,但是在控制器操作中,我仍然必须手动将 BlurbVm 字段映射回模糊,即使我认为上面的 setter 会处理它(我使用 Automapper对于其他领域)所以我很困惑为什么这不起作用。
此外,目前我正在控制器中进行验证,并且可能希望稍后对其进行重构以将它们移动到视图模型中,以便我可以使用 [Required] …
我正在尝试使用 colling 连接字符串连接到 postgresql。
dotnet ef dbcontext scaffold "Server=localhost; Port=xxxx; Database=i2ms; User Id=xxx; Password=xxx; SslMode=true" Npgsql.EntityFrameworkCore.PostgreSQL
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
System.ArgumentException: Couldn't set sslmode
Parameter name: sslmode ---> System.ArgumentException: Requested value 'true' was not found.
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at Npgsql.NpgsqlConnectionStringBuilder.set_Item(String keyword, Object value) in /home/roji/projects/npgsql/src/Npgsql/NpgsqlConnectionStringBuilder.cs:line 164
--- End of inner exception stack trace ---
at Npgsql.NpgsqlConnectionStringBuilder.set_Item(String keyword, Object value) in /home/roji/projects/npgsql/src/Npgsql/NpgsqlConnectionStringBuilder.cs:line 170
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at Npgsql.NpgsqlConnection.GetPoolAndSettings() in /home/roji/projects/npgsql/src/Npgsql/NpgsqlConnection.cs:line 178 …Run Code Online (Sandbox Code Playgroud) c# ×4
.net-core ×1
asp.net-core ×1
asp.net-mvc ×1
database ×1
many-to-many ×1
postgresql ×1
sql-server ×1