Haqz
Haqz
CC#
Created by Haqz on 12/22/2023 in #help
Prevent loading Many to Many relation further than first "level"
Hello, so currently i am struggling with .NET EF loading Many to Many (later called MtM) relation infinitely, which causes program to halt due to max depth being reached. I am looking for some way of avoiding that, but only after first "level". Example:
class A
{
public string PropertyA { get; set; }
public List<B> Bs = new();
}

class B
{
public string PropertyB { get; set; }
public List<A> As = new();
}

var test = new A{
PropertyA = "Test",
}

var test1 = new B{
PropertyB = "Test1",
}

test.Bs.Add(test1);
test1.As.Add(test);
class A
{
public string PropertyA { get; set; }
public List<B> Bs = new();
}

class B
{
public string PropertyB { get; set; }
public List<A> As = new();
}

var test = new A{
PropertyA = "Test",
}

var test1 = new B{
PropertyB = "Test1",
}

test.Bs.Add(test1);
test1.As.Add(test);
Assuming that these objects correspond to .NET EF models, this will cause infinite loop. test will have test1, that has test and so on. What i actually want is:
{
"PropertyA": "Test",
"Bs": [
{
"PropertyB": "Test1"
}
]
}
{
"PropertyA": "Test",
"Bs": [
{
"PropertyB": "Test1"
}
]
}
I've read that ChangeTracker.LazyLoadingEnabled = false;could help with that, but even having it set to false in my DbContext, it didnt help with the error. Only partial solution i found is to enable handling of loop references in Newtonsoft.JSON options, although it works, the issue of entities looping is still there For any help i thank in advance
2 replies
CC#
Created by Haqz on 9/7/2023 in #help
✅ xUnit | Saved entity not found.
Hello, this time coming with yet again problems with tests. So I'm testing my deleting route, so i create the related entity in database, get it's hash - the delete route takes hash as an path parameter, and pas it down to the repository for delete. The creation works, checked the context if it exists and it does, but for whatever reason it cannot be found and deleted. I think it may be problem with how i've set up the contexts. Repository
public void Delete(string hash)
{
var report = (from d in _dataContext.PaymentDatas
where d.Hash == hash
select d).Single();

_dataContext.PaymentDatas.Remove(report);
_dataContext.SaveChanges();
}
public void Delete(string hash)
{
var report = (from d in _dataContext.PaymentDatas
where d.Hash == hash
select d).Single();

_dataContext.PaymentDatas.Remove(report);
_dataContext.SaveChanges();
}
Test
private readonly DataContext context;

public InternalController(ITestOutputHelper output)
{
_factory = new CustomWebAppFactory();
_client = _factory.CreateClient();
this.output = output;

var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var dbOption = new DbContextOptionsBuilder<DataContext>()
.UseSqlite($"Data Source={Path.Join(path, "WebMinRouteGroup_tests.db")}");
context = new DataContext(dbOption.Options);
//context.Database.Migrate();

}
private readonly DataContext context;

public InternalController(ITestOutputHelper output)
{
_factory = new CustomWebAppFactory();
_client = _factory.CreateClient();
this.output = output;

var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var dbOption = new DbContextOptionsBuilder<DataContext>()
.UseSqlite($"Data Source={Path.Join(path, "WebMinRouteGroup_tests.db")}");
context = new DataContext(dbOption.Options);
//context.Database.Migrate();

}
39 replies
CC#
Created by Haqz on 9/6/2023 in #help
❔ Attaching record from one of multiple tables to another's entity property.
Heya, this time i'm struggling with Entity Framework. So I have main table PaymentDatas that holds mostly irrelevant data beside numeric value of which provider the rest of the data is stored. My goal is to have some sort of computed/virtual column, that when i query PaymentData, in property provider it has rest of the data pulled from specific provider. Thanks in advance!
22 replies
CC#
Created by Haqz on 9/5/2023 in #help
✅ xUnit | Repository not saving to database
Hello, so i started writing tests for my web api. Issue is that for whatever reason when i call the route, in test, with correct data, it doesnt seem to be saving it. It does save when I call the route normally via SwaggerUi. I tried mocking the save method in repositoey but no luck with that. Any help would be appricieted, thanks in advance. (Will post rest of related code in next message) CustomWebAppFactory
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);

builder.ConfigureTestServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<DataContext>));
if (descriptor != null) services.Remove(descriptor);

services.AddDbContext<DataContext>(o => o.UseInMemoryDatabase("InMemoryPayments"));

var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
using (var appContext = scope.ServiceProvider.GetRequiredService<DataContext>())
{

try
{
appContext.Database.EnsureCreated();

}
catch (Exception ex)
{
throw;
}
}

services.AddSingleton(PaymentDataRepository.Object);
});
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);

builder.ConfigureTestServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<DataContext>));
if (descriptor != null) services.Remove(descriptor);

services.AddDbContext<DataContext>(o => o.UseInMemoryDatabase("InMemoryPayments"));

var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
using (var appContext = scope.ServiceProvider.GetRequiredService<DataContext>())
{

try
{
appContext.Database.EnsureCreated();

}
catch (Exception ex)
{
throw;
}
}

services.AddSingleton(PaymentDataRepository.Object);
});
}
56 replies