First of all, excuse my english.
I am workink with monodroid on Visual Studio 2010. I'm creating some tables for SQLite and I have problems with a few of them. The problem is that this tables are created only with the primary key (ID) when it should have more fields.
I'm using the Krueger's SQLite class to manage the Data Layer.
Next line belongs to MyDataBase.cs, where I create the DataBase and call for create the tables.
CreateTable<MyTableClass>(); //This calls CreateTable on SQLite.cs
Below I show MyTableClass.cs
using MyApp.Core.DL.SQLite;
namespace MyApp.Core.BL
{
public class MyTableClass : Contracts.BusinessEntityBase
{
public MyTableClass ()
{
Field1 = 0;
Field2 = 0;
Field3 = "";
}
public int Field1;
public int Field2;
public string Field3;
}
}
This is the BusinessEntityBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyApp.Core.DL.SQLite;
namespace MyApp.Core.BL.Contracts
{
public abstract class BusinessEntityBase : IBusinessEntity
{
public BusinessEntityBase()
{
}
[PrimaryKey]
public int ID { get; set; }
}
}
And the interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyApp.Core.BL.Contracts
{
public interface IBusinessEntity
{
int ID { get; set; }
}
}
Rarely, the other tables include the fields and the code is similar to MyClass.cs, just change the fields. Any idea?
Thanks
You problem lies with the Field declarations. They should be Properties:
using MyApp.Core.DL.SQLite;
namespace MyApp.Core.BL
{
public class MyTableClass : Contracts.BusinessEntityBase
{
public MyTableClass ()
{
Field1 = 0;
Field2 = 0;
Field3 = "";
}
public int Field1 { get; set; }
public int Field2 { get; set; }
public string Field3 { get; set; }
}
}
Related
I'm trying to migrate from ASP.NET MVC on .NET 4.5 to ASP.NET Core 6,
I can't use System.Web.Security any more.
I also can't use HttpContext.Current.User.Identity.Name. Please suggest an alternative to implement the same functionality:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ibq.Wages.ManagementSystem.Models;
using Microsoft.AspNetCore.Http;
namespace Ibq.Wages.ManagementSystem.AppStart
{
public class CustomRoleProvider : RoleProvider
{
public int UserCompanyId { get; set; }
public string PayerQid { get; set; }
public string PayerEid { get; set; }
public bool OTPCompleted { get; set; }
public int? AuthorizationLevels { get; set; }
private PIBQWS_prodContext context = new PIBQWS_prodContext();
public override string ApplicationName { get; set; }
public CustomRoleProvider()
{
if(String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) == false)
UserCompany(HttpContext.Current.User.Identity.Name);
}
}
}
I'm starting to learn ASP.NET Core and I stuck in one error and don't understand what is problem.
It says:
IQueryable<Patron> does not contain a definition for FirstName and no extension method FirstName accepting a first argument of type IQueryable<Patron> could be found (are you missing a using directive or an assembly reference?"
So my code so far:
Patron.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace LibaryData.Models
{
public class Patron
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public DateTime DateOfBirth { get; set; }
public string TelephoneNumber { get; set; }
public virtual LibaryCard LibaryCard { get; set; }
public virtual LibaryBranch HomeLibaryBranch { get; set; }
}
}
CheckoutServices.cs
using LibaryData;
using LibaryData.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibaryData.Models;
namespace LibaryServices
{
public class ChekoutService : ICheckout
{
private LibaryContext _context;
public ChekoutService(LibaryContext context)
{
_context = context;
}
public string GetCurrentCheckoutPatron(int assetId)
{
var checkout = GetCheckoutByAssetId(assetId);
if(checkout == null)
{
return "";
}
var cardId = checkout.LibaryCard.Id;
var patron = _context.Patrons
.Include(p => p.LibaryCard)
.Where(p => p.LibaryCard.Id == cardId);
return patron.FirstName + " " + patron.LastName;
}
private Checkout GetCheckoutByAssetId(int assetId)
{
return _context.Checkouts
.Include(co => co.LibaryAsset)
.Include(co => co.LibaryCard)
.Where(co => co.LibaryAsset.Id == assetId)
.FirstOrDefault(co => co.LibaryAsset.Id ==assetId);
}
}
}
So in my class CheckoutServices in method GetCurrentCheckoutPatron I can not call a object patron.FirstName and patron.LastName
As you can see all classes are public, references is already there.
Any suggestion?
Replace .Where with .First to get a Patron and not a IQueryable<Patron>
Edit
Note that if you use .First, an exception can be raised if there's no match.
However, if you use .FirstOrDefault and there's no match, it will return null, so you'll get a NullReferenceException when trying to access patron.FirstName.
Be carefull to check for nulls
In your method GetCurrentCheckoutPatron the variable patron is an IQueryable. If you are expecting only single result from the query, you might want to call FirstOrDefault.
I need to return the results from a sql query to my mobile app.
I have a database on azure and a mobile service.
I have added a new custom API controller to my service, but I can't get any records back. I've been reading and reading and still do not understand what I am missing. Any help would be greatly appreciated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Mobile.Service;
using mytestService.Models;
using mytestService.DataObjects;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
using System.Threading.Tasks;
namespace mytestService.Controllers
{
public class SelectMapMarkersController : ApiController
{
public ApiServices Services { get; set; }
public class MapMarkers
{
public string MerchantName { get; set; }
public double MerchantLongitude { get; set; }
public double MerchantLatitude { get; set; }
public string MerchantMiniURL { get; set; }
public string Deal { get; set; }
public string CategoryDescription { get; set; }
}
// GET api/SelectMapMarkers
public List<MapMarkers> Get()
{
using (mytestContext context = new mytestContext())
{
// Get the database from the context.
var database = context.Database;
string sql = "select MerchantName, MerchantLongitude, MerchantLatitude, MerchantMiniUrl, Deal, CategoryDescription from mytest.tblMerchants m inner join mytest.tblMerchantDeals d on d.merchantId = m.merchantid inner join mytest.tblMerchantCategories mc on mc.merchantid = m.merchantid inner join mytest.tblCategories c on c.categoryid = mc.categoryid where m.MerchantActive = 1 and (d.dealstartdate <= getdate() and d.dealenddate >= getdate()) ";
List<MapMarkers> result = database.SqlQuery<MapMarkers>(sql).ToList<MapMarkers>();
return result;
}
}
}
}
This is what I would try...
Head over the database using SSMS and execute the t-sql line manually and see what the results are.
I want to use the Column data annotation as shown in the sample code below but the compiler (and also IntelliSense) do not seem to know that particular data annotation. I'm using EF 5 in Visual Studio 2010. I installed EF 5 using NuGet. The Required and MaxLength annotations are working.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
What am I missing?
Column is in:
using System.ComponentModel.DataAnnotations.Schema;
the following code:
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ConsoleApplication2
{
public class MyContext : DbContext
{
public IDbSet<Entity> Entities { get; set; }
}
public class Entity
{
public int Id { get; set; }
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
}
}
produces:
I am using asp.net mvc 4 i am using [maxlength(2)] in my model but it is not working on client side validation i am new to asp.net mvc .here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace RestrauntsMVC.Models
{
public class Restraunts
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
[MaxLength(2),MinLength(1)]
public int rating { get; set; }
[Required]
public string location { get; set; }
}
}
Answering myself to help future readers.I found out Maxlength and Minlength is for String not for integer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace RestrauntsMVC.Models
{
public class Restraunts
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
[Range(1,10,ErrorMessage="Rating must between 1 to 10")]
public int rating { get; set; }
[Required]
public string location { get; set; }
}
}
Like other mention is only for string so how about writing your own ValidationAttribute ?
using System.ComponentModel.DataAnnotations;
internal class MaxDigitsAttribute : ValidationAttribute
{
private int Max,
Min;
public MaxDigitsAttribute(int max, int min = 0)
{
Max = max;
Min = min;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!IsValid(value))
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
public override bool IsValid(object value)
{
// you could do any custom validation you like
if (value is int)
{
var stringValue = "" + (int)value;
var length = stringValue.Length;
if (length >= Min && length <= Max)
return true;
}
return false;
}
}