We are in the process of upgrading to RavenDB 2.5 and have run into a peculiar situation.
One of our unit tests is suddenly failing, and for no obvious reason.
Here is some simple code to reproduce the issue:
class Foo
{
public Guid Id { get; private set; }
public DateTime? ExpirationTime { get; set; }
public Foo()
{
Id = Guid.NewGuid();
ExpirationTime = null;
}
}
var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
};
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
session.Store(new Foo());
session.Store(new Foo());
session.SaveChanges();
}
So, now we have two documents in the database, both with ExpirationTime = null.
This is what happens when querying the database for these documents:
using (var session = documentStore.OpenSession())
{
var bar = session.Query<Foo>().Where(foo => foo.ExpirationTime == null).ToList();
Console.WriteLine("1. Number of documents: {0}", bar.Count);
bar = session.Query<Foo>().Where(foo => foo.ExpirationTime == null ||
foo.ExpirationTime > DateTime.Now).ToList();
Console.WriteLine("2. Number of documents: {0}", bar.Count);
bar = session.Query<Foo>().Where(foo => foo.ExpirationTime == null |
foo.ExpirationTime > DateTime.Now).ToList();
Console.WriteLine("3. Number of documents: {0}", bar.Count);
}
The output from these queries are as follows:
1. Number of documents: 2
2. Number of documents: 0
3. Number of documents: 2
This doesn't seem correct to me... I would expect also number 2. to give 2.
I ran the same queries against a RavenDB server, and got the expected results, so it seems like this is an issue with the EmbeddableDocumentStore.
While testing this I also found some strange behavior when using the logical or operator in other cases. Sometimes using foo.HasValue would give different result than foo != null, but that might be related to the same issue.
Anyone else that have experienced this problem? Known bug?
This was a bug in RavenDB related to sorting on null fields.
Fixed in the next build
Related
I am using Nest 6.5.4. I am not able to perform Update with script on a particular document in an index.
I have tried many ways but I am getting a syntax error.
My query is as follows.
var clientProvider = new ElasticClientProvider();
var projectModel = new ProjectModel();
var res = clientProvider.Client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Id(projectId)
.Script(script=>script.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
It is throwing an error "Update Descriptor does not have a definition for Id"
The same query is working when tried in Kibana
POST attachment_index/attachments/1/_update
{
"script": {
"source":"ctx._source.fileInfo.fileViewCount += 1"
}
}
I dont know where I am getting error.
client.UpdateAsync<ProjectModel, object>(
new DocumentPath<ProjectModel>(id),
u => u
.Index(ConfigurationManager.AppSettings.Get("indexname"))
.Type(ConfigurationManager.AppSettings.Get("indextype"))
.Doc(ProjectModel)
.DocAsUpsert()
.Refresh(Elasticsearch.Net.Refresh.True));
This will work and let me know if you are still facing any issues.
There is no .Id() method on UpdateDescriptor<T, TPartial> because an id is a required parameter for an Update API call, so this constraint is enforced through the constructors.
The first parameter to .Update<T>(...) is a DocumentPath<T> from which an index, type and id can be derived for the update API call. If the ProjectModel CLR POCO has an Id property with a value, this will be used for Id of the call. For example
public class ProjectModel
{
public int Id { get; set; }
}
var client = new ElasticClient();
var projectModel = new ProjectModel { Id = 1 };
var updateResponse = client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
which results in
POST http://localhost:9200/attachment_index/attachments/1/_update
{
"script": {
"source": "ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"
}
}
If you want to explicitly specify the Id, you can pass the value for DocumentPath<T>
var updateResponse = client.Update<ProjectModel>(1, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
I'm using Entity Framework Core 2.1.4 with Oracle 11 database and Devart.Data.Oracle.EFCore provider.
Database first approach.
I want to get from sequence value for ID column (primary key) on inserting without setting this explicitly every time.
So, based on similar infos with SQL Server, I did it as following:
Entity
public class Foo
{
public int Id { get; set; }
public double Value { get; set; }
}
Mapping (OnModelCreating method)
modelBuilder.HasSequence<int>("SEQ_FOOS", schema: "SCHEMA")
.StartsAt(1)
.IncrementsBy(1);
modelBuilder.Entity<Foo>(entity =>
{
entity.ForOracleToTable("FOOS");
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ForOracleHasColumnName("ID").IsRequired().ForOracleHasDefaultValueSql("SELECT SEQ_FOO.NEXTVAL FROM DUAL");
entity.Property(e => e.Value).HasColumnName("VALUE");
});
Adding value:
using (var dbContext = new FooDbContext())
{
var foo = new Foo()
{
Value = 5
};
dbContext.Foos.Add(foo);
dbContext.SaveChanges();
}
On SaveChanges:
OracleException: ORA-01400: cannot insert NULL into ("SCHEMA"."FOOS"."ID")
I also logged EF query. As you can see, there is no ID column in insert:
INSERT INTO SCHEMA.FOOS (VALUE)
VALUES (:p0)
I was trying to use simply SEQ_FOO.NEXTVAL instead of full select or default EF methods (like HasDefaultValueSql) but nothing worked. Even if I type:
ForOracleHasDefaultValueSql("asdasd");
There is no errors with this - only the same exception as above. It seems like EF never call that SQL.
Am I missing something important? Or maybe it's internal Devart problem?
Ok, I have solution. It seems we need to use ValueGenerator. My implementation below.
Mapping
entity.Property(e => e.Id)
.ForOracleHasColumnName("ID")
.IsRequired()
.ValueGeneratedOnAdd()
.HasValueGenerator((_, __) => new SequenceValueGenerator(_defaultSchema, "SEQ_FOOS"));
SequenceValueGenerator (please note that ValueGenerator is EF Core type)
internal class SequenceValueGenerator : ValueGenerator<int>
{
private string _schema;
private string _sequenceName;
public SequenceValueGenerator(string schema, string sequenceName)
{
_schema = schema;
_sequenceName = sequenceName;
}
public override bool GeneratesTemporaryValues => false;
public override int Next(EntityEntry entry)
{
using (var command = entry.Context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = $"SELECT {_schema}.{_sequenceName}.NEXTVAL FROM DUAL";
entry.Context.Database.OpenConnection();
using (var reader = command.ExecuteReader())
{
reader.Read();
return reader.GetInt32(0);
}
}
}
}
It seems to work as I needed.
Mapping:
private void FooMapping(ModelBuilder modelBuilder)
{
//modelBuilder.HasSequence<int>("SEQ_FOOS", schema: "SCHEMA")
// .StartsAt(1)
// .IncrementsBy(1);
modelBuilder.Entity<Foo>(entity =>
{
entity.ForOracleToTable("FOOS");
entity.HasKey(e => e.Id);
//entity.Property(e => e.Id).ForOracleHasColumnName("ID").IsRequired().ForOracleHasDefaultValueSql("SELECT SEQ_FOO.NEXTVAL FROM DUAL");
entity.Property(e => e.Value).HasColumnName("VALUE");
});
}
Code:
// https://www.devart.com/dotconnect/oracle/docs/?dbmonitor.html
var monitor = new OracleMonitor() { IsActive = true };
using (var dbContext = new FooModel())
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
var foo = new Foo()
{
Value = 5
};
dbContext.Foos.Add(foo);
dbContext.SaveChanges();
}
Check SQL generated in dbMonitor. Is that what you need?
Did not figure this out. I have similar problem on Oracle 18C - I need to fill PK in the table, PK is a NUMBER, not IDENTITY (this is obviously a defect and will be changed later on, but now I have to deal with that since I don't have rights to change DB structure, however I need to prepare CRUD demo). I don't want to use some C# value generator, but instead - DB remedy. So I tried to use the following (but it did not work - the expression is ignored):
b.HasKey(x => x.Id);
b.Property(x => x.Id).HasColumnName("C_LICENCE").IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("select round(dbms_random.value(100000, 999999)) from dual");
I suspect it's probably because int primary column is never null :) But anyway, I need to somehow force it to be generated via SQL always.
I've looked at the various solutions here but none of them seem to work for me, probably because I'm too new to all this and am groping in the dark a bit. In the code below, the object "appointment" contains some basic LDAP information. From a list of such objects I want to be able to get a single record, based on employee id. I hope the code here is sufficient to illustrate. FTR, I've tried various formulations, including trying to use from and a select. All fail with the error given in the Title above.
IQueryable<appointment> query = null;
foreach(var record in results)
{
BoiseStateLdapDataObject record1 = record;
query = db.appointments.Where(x => x.student_id == record1.Values["employeeid"]);
}
if (query != null)
{
var selectedRecord = query.SingleOrDefault();
}
Try to move employee id getting out of query:
IQueryable<appointment> query = null;
foreach(var record in results)
{
var employeeId = record.Values["employeeid"];
query = db.appointments.Where(x => x.student_id == employeeId);
}
if (query != null)
{
var selectedRecord = query.SingleOrDefault();
}
So I'm working through a simplified example of my hoped-for database that has the following tables:
Contractors: Id, ContractorName
Types: Id, TypeName
CoverageZips: ContractorId, Zip
TypesForContractors: ContractorId, TypeId
where contractors can have many zips and types and types and zips can have many contractors (many-to-many).
I'm trying to:
do a search for contractors in a certain zip code
then load the types for those contractors.
The SQL for the first part would probably look like:
SELECT * FROM dbo.Contractors WHERE Id IN
(SELECT ContractorId FROM dbo.CoverageZips WHERE Zip = 12345)
Here's what I have for the first part in Simple.Data. It's working, but I feel like I'm missing some of the beauty of Simple.Data...
List<int> contractorIds = new List<int>();
foreach(var coverage in _db.CoverageZips.FindAllByZip(zip)) {
contractorIds.Add((int)coverage.ContractorId);
}
var contractors = new List<dynamic>();
if (contractorIds.Count > 0) {
contractors = _db.Contractors.FindAllById(contractorIds).ToList<dynamic>();
}
return contractors;
That's working ok until I try part 2:
public dynamic GetAllForZip(int zip) {
List<int> contractorIds = new List<int>();
foreach(var coverage in _db.CoverageZips.FindAllByZip(zip)) {
contractorIds.Add((int)coverage.ContractorId);
}
var contractors = new List<dynamic>();
if (contractorIds.Count > 0) {
contractors = _db.Contractors.FindAllById(contractorIds).ToList<dynamic>();
}
foreach (var contractor in contractors) {
// Exception occurs here on second iteration
// even though the second contractor was originally in the contractors variable
contractor.types = GetTypesForContractor((int)contractor.Id);
}
return contractors;
}
public dynamic GetTypesForContractor(int id) {
var types = new List<dynamic>();
if (id > 0) {
List<int> typeIds = new List<int>();
foreach (var typeForContractor in _db.TypesForContractor.FindAllByContractorId(id)) {
typeIds.Add((int)typeForContractor.TypeId);
}
if (typeIds.Count > 0) {
types = _db.ContractorTypes.FindAllById(typeIds).ToList<dynamic>();
}
}
return types;
}
I set a breakpoint and everything works ok for the first iteration showing , but is failing on the second with the following exception:
Index was out of range. Must be non-negative and less than the size of the collection.
tl;dr
I'm not sure how to properly use many-to-many relationships with Simple.Data and something weird is happening when I try my method more than once
I don't know what's happening with that exception and will investigate today.
You are missing some beauty, though. Assuming you have referential integrity configured on your database (which of course you do ;)), your methods can be written thus:
public dynamic GetAllForZip(int zip) {
var contractors = _db.Contractors
.FindAll(_db.Contractors.ContractorZips.Zip == zip)
.ToList();
foreach (var contractor in contractors) {
contractor.Types = GetTypesForContractor((int)contractor.Id);
}
return contractors;
}
public dynamic GetTypesForContractor(int id) {
return _db.ContractorTypes
.FindAll(_db.ContractorTypes.TypesForContractor.ContractorId == id)
.ToList();
}
Update!
As of 1.0.0-beta3, eager-loading across many-to-many joins is supported, so now you can do this:
public dynamic GetAllForZip(int zip) {
return _db.Contractors
.FindAll(_db.Contractors.ContractorZips.Zip == zip)
.With(_db.Contractors.TypesForContractor.ContractorTypes.As("Types"))
.ToList();
}
And that executes as a single SQL select to make your DBA happy like rainbow kittens.
I have a piece of code which combines an in-memory list with some data held in a database. This works just fine in my unit tests (using a mocked Linq2SqlRepository which uses List).
public IRepository<OrderItem> orderItems { get; set; }
private List<OrderHeld> _releasedOrders = null;
private List<OrderHeld> releasedOrders
{
get
{
if (_releasedOrders == null)
{
_releasedOrders = new List<nOrderHeld>();
}
return _releasedOrders;
}
}
.....
public int GetReleasedCount(OrderItem orderItem)
{
int? total =
(
from item in orderItems.All
join releasedOrder in releasedOrders
on item.OrderID equals releasedOrder.OrderID
where item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}
I am getting an error I don't really understand when I run it against a database.
Exception information:
Exception type: System.NotSupportedException
Exception message: Local sequence cannot be used in LINQ to SQL
implementation of query operators
except the Contains() operator.
What am I doing wrong?
I'm guessing it's to do with the fact that orderItems is on the database and releasedItems is in memory.
EDIT
I have changed my code based on the answers given (thanks all)
public int GetReleasedCount(OrderItem orderItem)
{
var releasedOrderIDs = releasedOrders.Select(x => x.OrderID);
int? total =
(
from item in orderItems.All
where releasedOrderIDs.Contains(item.OrderID)
&& item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}
I'm guessing it's to do with the fact
that orderItems is on the database
and releasedItems is in memory.
You are correct, you can't join a table to a List using LINQ.
Take a look at this link:
http://flatlinerdoa.spaces.live.com/Blog/cns!17124D03A9A052B0!455.entry
He suggests using the Contains() method but you'll have to play around with it to see if it will work for your needs.
It looks like you need to formulate the db query first, because it can't create the correct SQL representation of the expression tree for objects that are in memory. It might be down to the join, so is it possible to get a value from the in-memory query that can be used as a simple primitive? For example using Contains() as the error suggests.
You unit tests work because your comparing a memory list to a memory list.
For memory list to database, you will either need to use the memoryVariable.Contains(...) or make the db call first and return a list(), so you can compare memory list to memory list as before. The 2nd option would return too much data, so your forced down the Contains() route.
public int GetReleasedCount(OrderItem orderItem)
{
int? total =
(
from item in orderItems.All
where item.ProductID == orderItem.ProductID
&& releasedOrders.Contains(item.OrderID)
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}