.NET model with FK(foreign key) not working

.NET model with FK(foreign key) not working

joakimvfjoakimvf Posts: 9Questions: 2Answers: 0

Hi,

I've been reading the .NET manual several time without success but I guess I'm missing something.

I have the following .NET model:

public class Närradio
    {
        public Närradio() { }

        [Key]
        public int Id { get; set; }

        public int ParterID { get; set; }

        [ForeignKey("ParterID")]
        public virtual Parter Parter { get; set; }

        public string Startdatum { get; set; }

        public string Slutdatum { get; set; }
    }

And my API Controller:

        [HttpGet]
        [HttpPost]
        public IHttpActionResult GetTest()
        {
            var request = HttpContext.Current.Request;
            using (var db = new Database("sqlserver", _connectionString))
            {
                var editor = new Editor(db, "Närradio", "Id")
                    .Model<Närradio>()
                    .Process(request)
                    .Data();

                return Json(editor);
            }
        } 

When I try it with Postman I get the following error:

"error": "Invalid column name 'Parter'.",

How does Datatables handle public virtual Parter Parter? I don't see something similiar being used in the examples.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,894Questions: 1Answers: 10,747 Site admin
    Answer ✓

    How does Datatables handle public virtual Parter Parter?

    It doesn't I'm afraid. The types used in the model need to be directly mappable to JSON - i.e. they are basically restricted to numbers and strings. Custom types are not something that is currently supported.

    Allan

  • joakimvfjoakimvf Posts: 9Questions: 2Answers: 0

    Hi again,

    Is there another way to display data with Datatables that uses FK with .NET/C#? Or does Datatables have a way to handle relational databases?

  • allanallan Posts: 64,894Questions: 1Answers: 10,747 Site admin

    If you are using the Editor .NET libraries (which it appears you are) you can use the leftJoin method to join between tables if that is what you mean.

    Allan

This discussion has been closed.