小编Ger*_*old的帖子

是否可以搜索dbml图?

这对几个表来说不是问题,但是当我在dbml图上有大量表时,我必须进行平移和缩放才能找到我要查找的表.有没有办法搜索dbml图,以便它自动转到我搜索的表?

linq-to-sql visual-studio

10
推荐指数
2
解决办法
2182
查看次数

包含属性但排除该属性的属性之一

假设我的一个控制器中有这样的方法:

[Route("api/Products")]
public IQueryable<Product> GetProducts() {
    return db.Products
             .Include(p => p.Category);
}
Run Code Online (Sandbox Code Playgroud)

使用这个我可以从数据库中获取产品并包含其Category属性.

在我的CategoryControllerI中有这个方法:

[Route("api/Categories")]
public IQueryable<Category> GetCategories() {
    return db.Categories
             .Include(c => c.Parent)
             .Include(c => c.Products)
             .Include(c => c.SubCategories);
}
Run Code Online (Sandbox Code Playgroud)

当我向CategoryController发送GET请求时,它按预期工作,我得到类别,其父级,产品和子类别.但是,当我向ProductController发送GET请求时,我不想将所有产品都包含在所请求产品的类别中,我只需要有关该类别的基本信息.

那么,我如何让GetProducts()返回数据库中的产品,包括每个产品的Category属性,但不包括该类别的Products列表属性,仍然保留其他属性,如id,title等?

谢谢.

c# entity-framework asp.net-web-api2

10
推荐指数
1
解决办法
5647
查看次数

无法更新数据库以匹配当前模型,因为存在未决更改

我有一个在 Visual Studio 2013 环境中构建的项目,其中 Db 首先使用 EF 5 代码构建。我的 API 已经运行良好很长时间了,但突然间我开始收到以下错误消息:

无法更新数据库以匹配当前模型,因为存在未决更改且自动迁移已禁用。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移。

当我尝试到达 API 的终点时。我尝试添加一个新的迁移,然后更新数据库,但仍然出现错误。然后我删除整个数据库并使用 EF 重新创建。我的 API 的端点开始正常工作,但随后我又开始在网页上收到此错误。我在配置文件中将自动迁移设置为 true。我真的不知道为什么这一次又一次地发生。这让我真的很沮丧。这是错误的完整堆栈跟踪:

[AutomaticMigrationsDisabledException:无法更新数据库以匹配当前模型,因为存在挂起的更改并且自动迁移被禁用。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移。]
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +579 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +445
System.Data.Entity.Migrations.<>c__DisplayClassc.b__b() +13
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +12 YourTimeSite.Global.ApplyDatabaseMigrations() in c:\Users\Ahmed\Desktop \YourTimeSite\YourTimeSite\Global.asax.cs:55
YourTimeSite.Global.Application_Start(Object sender, EventArgs e) in c:\Users\Ahmed\Desktop\YourTimeSite\YourTimeSite\Global.asax.cs:32

[HttpException (0x80004005): 无法更新数据库以匹配当前模型,因为存在挂起的更改并且禁用了自动迁移。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9966013
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[]
系统处理程序) +1 .Web.HttpApplication.InitSpecial(HttpApplicationState …

asp.net-mvc entity-framework ef-code-first

10
推荐指数
2
解决办法
3万
查看次数

创建编辑器失败。HRESULT 异常:0x80070057 (E_INVALIDARG)

创建编辑器失败。参数不正确。(HRESULT 异常:0x80070057 (E_INVALIDARG))

\n

错误

\n

脚本迁移 - 来自“PreviousMigration”

\n

将 Visual Studio Community 2022 从版本 17.8.6 更新到 17.9.0 导致错误。

\n

脚本迁移:调用 COM 组件返回了错误 HRESULT E_FAIL。

\n

行:1 字符:1

\n

**解决方案:

\n

我尝试从系统文件夹中删除用户和框架的所有临时文件,但它对我不起作用,然后我将xc2xa0降级到以前的xc2xa0Visual Studio Community 2022 版本 17.8.6,它可以工作。

\n

我尝试从系统文件夹中删除用户和框架的所有临时文件,但它对我不起作用,然后我将xc2xa0降级到以前的xc2xa0Visual Studio Community 2022 版本 17.8.6,它可以工作。

\n

visual-studio entity-framework-core entity-framework-migrations ef-core-6.0

10
推荐指数
1
解决办法
468
查看次数

Resharper有大或*非常*大文件

我只是想知道你使用resharper的经验.我们有一个非常繁重的dbml文件,因为我们的数据库有很多表,每次我需要打开该文件时,我开始从resharper获得大量异常.以前有人有这个问题吗?如果是的话,你做了什么来解决这个非常烦人的问题?

c# resharper exception linq-to-sql

9
推荐指数
1
解决办法
787
查看次数

如何使用集合

任何人都可以写迷你指南,解释如何使用EF中的集合吗?

例如,我有以下模型:

public class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
    public List<PostComment> Comments { get; set; }
}

public class PostComment
{
    public int Id { get; set; }
    public BlogPost ParentPost { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和上下文类:

public class PostContext : …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core

9
推荐指数
1
解决办法
2617
查看次数

INSERT语句与FOREGIGN KEY约束冲突

我是一个绿色的asp.net开发人员,我正在使用我的项目的最新实体框架.我有一个问题是使用自动生成的值为我的数据库播种(我认为).这是确切的错误.

在此输入图像描述

这是代码:

public class Address
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AddressId { get; set; }
// some more properties
}

public class ApplicationUser : IdentityUser
{
        [ForeignKey("Address")]        
    public int AddressId { get; set; }
    public Address Address { get; set; }

// some more properties
}

public class Shelter
{
        [Required]
        [ForeignKey("Address")]
        public int AddressId { get; set; }
        public Address Address { get; set; }
// some more properties
}
//seed
private void CreateShelters()
{
    EntityEntry<Address> MspcaAddress = DbContext.Addresses.Add(new …
Run Code Online (Sandbox Code Playgroud)

asp.net entity-framework entity-framework-core

9
推荐指数
1
解决办法
337
查看次数

尝试使用 EF Core 7 添加迁移时,“片段”没有实现异常

当我尝试使用 EF Core (7.0.0-rc.1.22426.7) 添加迁移时,我在控制台中看到以下错误:

System.TypeLoadException: Method 'Fragment' in type 'Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper' from assembly 'Microsoft.EntityFrameworkCore.Design, Version=6.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
   at Microsoft.EntityFrameworkCore.Design.DesignTimeServiceCollectionExtensions.<>c__DisplayClass0_0.<AddEntityFrameworkDesignTimeServices>b__0(ServiceCollectionMap services)
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAddProviderSpecificServices(Action`1 serviceMap)
   at Microsoft.EntityFrameworkCore.Design.DesignTimeServiceCollectionExtensions.AddEntityFrameworkDesignTimeServices(IServiceCollection services, IOperationReporter reporter, Func`1 applicationServiceProviderAccessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.CreateServiceCollection(DbContext context)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Method 'Fragment' in type 'Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper' from assembly 'Microsoft.EntityFrameworkCore.Design, Version=6.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core entity-framework-core-migrations ef-core-7.0

9
推荐指数
2
解决办法
7971
查看次数

DBML定制与再生

我有一个定义了很多外键关系的数据库.当我将这些FK连接中涉及的任何表拖到DBML编辑器中时,为了机器生成DBML文件,这些都将表示为关联.

从这里我可以对这些关联进行任何更改:我可能希望关联的父端internal而不是public使得JSON序列化程序(比如说)不会被循环引用捕获; 或在表之间的连接FormFormAnswer,我可能要被称为子属性Answers,而不是生成的机器FormAnswers.

现在,如果更改了数据库设计,并且我想更新DBML以反映此更改,那么这些自定义似乎需要我跟踪每个更改并手动更新它(添加属性,设置它的源,源数据类型, C#数据类型......)

这可能是一个相当繁琐的过程; 我问的是,是否有任何方法可以实现自动化.

1.我可以在SQL服务器上反映这些更改吗?

看起来,理想的解决方案是,如果有任何方法可以直接在SQL Server数据库图表中制作这些规范,那么完全重新生成DBML文件(删除所有内容并将其拖到DBML编辑器上)将会得出完全相同的结果.

怀疑我已经知道上述情况,如果可以实现,我很乐意和解:

2.我可以将这些更改提取到自己的类中吗?

由于所有Linq to SQL实体都是作为部分类生成的,我想了一段时间我可以创建一个新文件,我手动维护,我可以将所有更改复制到所提到的文件中.

因此,每当我更改关联时,我都会深入了解designer.cs代码,剪切修改后的关联,并将其粘贴到我自己的文件中.重新生成后,我会期望任何重复的编译器错误,并轻松地逐步执行并从DBML中删除这些关联.这里的问题是关联似乎只是属性的属性.如果Form有一个被调用的属性Answers,并且DBML生成器将尝试创建一个被调用的属性FormAnswers,那么结果Form对象将只具有这两个属性,这根本不是我想要的.

有没有人对这些解决方案有任何好运?或者,如果您知道处理问题的任何其他方式,我愿意接受建议.

.net linq-to-sql

8
推荐指数
2
解决办法
1602
查看次数

从实体框架核心中包含的属性中选择特定属性

我使用Entity Framework Core 2.0并拥有这两个类:

新闻

public class News
{
    public int Id { get; set; }
    public string Title{ get; set; }
    public string Text { get; set; }
    public DateTime ReleaseDate{ get; set; }
    public int AuthorID{ get; set; }
    public Author Author{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

作者

public class Author 
{
    public Author ()
    {
     News = new List<News>();
    }

    public int Id { get; set; }
    public string Username{ get; set; }
    public string Firstname{ get; set; …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core

8
推荐指数
2
解决办法
6408
查看次数