C# SQLite Extensions InsertWithChildren doesn't work why? - c#

i have a Problem with my SQLite-Database. I use the SQLite.Net and SQLiteNetExtensions PCL NuGets and followed this excample: Link
I have a connection and it insert a stock but not the valuation list.
My copy paste code:
var valuation1 = new Valuation
{
Price = 15,
Time = DateTime.Now,
};
var valuation2= new Valuation
{
Price = 22,
Time = DateTime.Now,
};
var euro = new Stock
{
Symbol = "€",
Valuations = new List<Valuation> { valuation1, valuation2 }
};
connection.CreateTable<Stock>();
connection.CreateTable<Valuation>();
connection.InsertWithChildren(euro);
var stockList = c.GetAllWithChildren<Stock>();
In Stocklist is a Stock but in Stock is no Valuation :(
usings:
using System; using System.Collections.Generic;
using System.Linq;
using SQLiteNetExtensions.Extensions;
using TestSQLite.Models;
Models:
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.Generic;
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[OneToMany] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
}
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))] // Specify the foreign key
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
[ManyToOne] // Many to one relationship with Stock
public Stock Stock { get; set; }
}
Any ideas or suggestions what i could do to make it work?
EDIT:
I just tried some things and if i inseret it by my self like this:
c.Insert(euro);
var e = c.Find<Stock>(s => s.Symbol == "€");
var valuation1 = new Valuation
{
Price = 15,
Time = DateTime.Now,
StockId = e.StockId,
Stock = e
};
var valuation2 = new Valuation
{
Price = 22,
Time = DateTime.Now,
StockId = e.StockId,
Stock = e
};
c.Insert(valuation1);
c.Insert(valuation2);
var stockList = c.GetAllWithChildren<Stock>();
i get a correct stock "euro" and valutations in it! What is my mistake at InsertWithChildren? Or is there anything wrong with my models?

Related

I am getting an error while producing objects

I'm developing a project with N-tier architecture. I got errors while generating a new object in the main function.I can not give the features of car1. The entire text is underlined in red and I get these errors "CS1922: Cannot initialize type 'Car' with a collection initializer because it does not implement 'System.Collections.IEnumerable'" and "CS0747: Invalid initializer member declator".
using Business.Concrete;
using System;
using Entities.Concrete;
using DataAccess.Concrete.EntityFramework;
static void Main(string[] args)
{
Car car1 = new Car
{
car1.Id = 4,
car1.BrandId = 1,
car1.ColourId = 2,
car1.ModelYear = 1990,
car1.Name = "King",
car1.DailyPrice = 150,
car1.Description = "Best"
};
CarManager carManager = new CarManager(new EfCarDal());
Console.ReadLine();
}
Entities\Concrete\Car.cs
using Core.Entities;
using System;
using System.Collections.Generic;
using System.Text;
namespace Entities.Concrete
{
public class Car : IEntity
{
public int Id { get; set; }
public int BrandId { get; set; }
public int ColourId { get; set; }
public string Name { get; set; }
public int ModelYear { get; set; }
public int DailyPrice { get; set; }
public string Description { get; set; }
}
}
You don't need the car1. access when initializing within {} like so:
Car car1 = new Car
{
Id = 4,
BrandId = 1,
ColourId = 2,
ModelYear = 1990,
Name = "King",
DailyPrice = 150,
Description = "Best"
};

Query MongoDB in C# using LINQ

I have simple class which represents fields in MongoDB document
class Measurement
{
public ObjectId id { get; set; }
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
I'm trying to get documents matching my conditions using
var collection = database.GetCollection<Measurement>(mongoCollectionName);
var query = from a in collection.AsQueryable<Measurement>()
where a.dt > 100
select a;
When where condition is removed i do receive all documents but with condition none. Response says there's no matching documents but there are (example dt=1538555)
query looks like this {aggregate([{ "$match" : { "dt" : { "$gt" : 100 } } }])}.
I build my example using response from this thread and mongodb documentation
MongoDB C# Aggregation with LINQ
I would be grateful with solving probably my stupid mistake
i don't use the c# driver directly anymore so my solution is using MongoDAL.
using System;
using System.Linq;
using MongoDAL;
namespace Example
{
class Measurement : Entity
{
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
class Program
{
static void Main(string[] args)
{
new DB("measurements");
var measurement1 = new Measurement
{
s = 10,
ml = 20,
dt = 100,
p = new int[] { 1, 2, 3, 4, 5 }
};
var measurement2 = new Measurement
{
s = 11,
ml = 22,
dt = 200,
p = new int[] { 1, 2, 3, 4, 5 }
};
measurement1.Save();
measurement2.Save();
var result = (from m in DB.Collection<Measurement>()
where m.dt > 100
select m).ToArray();
Console.ReadKey();
}
}
}

You can not implicitly convert "RESTfullApi.Models.Product []" to "RESTfullApi.Models.Product"

i'm trying to learn how to create Web Api, and i have an error.
I don't know what to do, first it required to use sufix M when i used it, VS show me error:
You cannnot implicitly convert "RESTfullApi.Models.Product []" to
"RESTfullApi.Models.Product"
I was trying to find answer on the internet but nothing explain this case.
Maybe you know what is wrong with it?
This is tutorial in which i practice:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
ProductsController.cs
using RESTfullApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace RESTfullApi.Controllers
{
public class ProductsController : ApiController
{
Product products = new Product[]
{
new Product { Id = 1, Name = "Pizza Margarita", Category = "Pizza", Price = 13.00M },
new Product { Id = 2, Name = "Pizza Double Cheese", Category = "Pizza", Price = 17.00M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
}
}
Product.cs (Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RESTfullApi.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}
Message is self explanatory. Change products to
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Pizza Margarita", Category = "Pizza", Price = 13.00M },
new Product { Id = 2, Name = "Pizza Double Cheese", Category = "Pizza", Price = 17.00M }
};

Create unique postalcode and country generator

I am working an C# application where I have to add test employee data to a MongoDB collection.
There is a working connection already and some predefined list where the program is going to loop through to insert data. However, I am stuck add the point where I'd have to add an address.
I need a piece of code to create at least 10.000 unique postalcodes without using a library if possible.
using System;
using System.Collections.Generic;
using MongoDB.Driver;
using MongoDB.Bson;
namespace Application
{
class Program
{
public class Employee
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Street { get; set; }
public string Country { get; set; }
public string Postalcode { get; set; }
}
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("testdb");
var collection = db.GetCollection<Employee>("testcollection");
List<string> names = new List<string>()
{
"John Smith",
"Matthew Williams",
"David Harris",
"Christopher Martin",
"Paul Shark"
};
Random random = new Random();
for (int i = 0; i < 5; i++)
{
int nameIndex = random.Next(0, 5);
int projectIndex = random.Next(0, 3);
int ageIndex = random.Next(18, 67);
string nameValue = names[nameIndex];
Employee employee = new Employee
{
BSN = "BSN" + i,
Name = nameValue,
Age = ageIndex
};
collection.Save(employee);
}
}
}
}
When I test forms I grab Faker.net from Nuget. This will generate random data for everything you need.
The implementation is pretty simple. Going from memory here so your mileage may vary but you get the idea.
var address1 = Faker.address.address1;

Trouble assigning a value to this variable

Using this code I intend to populate the variables.
http://www.dreamincode.net/forums/xml.php?showuser=146038
//Load comments.
var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments = (from comment in commentsXML.Descendants("comment")
select new Comment()
{
ID = comment.Element("id").Value,
Text = comment.Element("text").Value,
Date = comment.Element("date").Value,
UserWhoPosted = comment.Element("user").Value
}).ToList();
The problem is UserWhoPosted is an object of the User.cs class POCO I made:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharpDIC.Entities
{
public class Friend
{
public string ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Photo { get; set; }
}
}
How would I populate that object?
I haven't tested this but can't you instantiate the Friend object when your populating the Comment object?
var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments =
(
from comment in commentsXML.Descendants("comment")
select new Comment()
{
ID = comment.Element("id").Value,
Text = comment.Element("text").Value,
Date = comment.Element("date").Value,
UserWhoPosted = new Friend()
{
ID = comment.Element("user").Descendants("id"),
Name = comment.Element("user").Descendants("name"),
Url = comment.Element("user").Descendants("url"),
Photo = comment.Element("user").Descendants("photo")
}
}
).ToList();

Categories

Resources